The AI for the moving enemies in Journey of Haha has been done for a few months now, but it took a great deal of effort to get it to work how I wanted it. It was a balance of having the enemies be mostly dumb, but smart enough to pose a threat to the player if they spotted the player.

The first thing I needed to do was have the enemies avoid obstacles. I did this by having the enemies cast a box of linecasts around them. Internally, they look like this:


Those white lines around the Skull enemy are the linecasts, and they determine if the enemy can move up, down, left, or right. The enemy keeps an internal list of directions it can move in. The starting direction it moves in can be specified, but if nothing is specified, it moves in a random direction. This is enough to get the enemy to successfully navigate out of this series of corridors:


The end result is that the enemies will move in a direction until they can no longer continue. Once an obstacle is detected, they will pick a new direction. However, the new direction is not entirely random. When a new direction is picked it will not be the same direction the enemy came from, unless there is no other alternative.

That describes how the enemies move and avoid obstacles. However, this movement would be kind of boring if that were all the enemy did. It would be easy for the player to avoid the enemy since all it would do is move in a straight line until it can’t move in that direction anymore. To make the enemies more interesting (and deadly!), they have a second behavior in their AI.

The enemies will “look” in all four directions. If they see the player, they will immediately turn in the player’s direction. Internally I call this “seeking” and it’s something I can disable for some enemies.

Here you can see an enemy moving straight up, and then turning left because it sees the player:


I had considered using the A* algorithm for my enemies to more effectively seek out the player, but I decided against it. I didn’t want the enemies to be that aggressive in how they seek. The ideal behavior for them to have, in this game, is to move around somewhat randomly, but if they spot the player, they move in the player’s direction.

I’m pretty pleased with how the enemies behave, and it’s great fun to see them roam around and chase my play testers down.