Artifact 5, AI states Idle, Scared and Hunting.

The last blog i wrote was kind of incomplete. To try and make up for it I will explain 3 of the AI states in our game.

We needed the enemy fishes to move around a bit in the level. So I started to make the Idle state. I remembered the way I tried to have an enemy to move in a rectangular pattern in the last project I made. I wasn’t really satisfied with that. Last time i made a struct in the enemy class containing two floats x and y to place a couple of points for it to move towards and change direction when it passed the points. That was pretty buggy you could say, sometimes the enemy moved a little past the points and sometimes it turned before it had reached the points.

I wanted to do the Idle movement differently this time. Before I started working on the AI State Machine, there was a temporary system of enemy fishes moving left and then right based on a timer. I reused that system and added randomness to it and also made them go up and down. So now the small fishes move around everywhere around the map. it looks like this:


The Idlestate uses a pointer to the enemy to call an enemy function, Idle(). Then it checks for distance to the player fish. If they are within 300 pixels the grey fish will be scared and change state to Scared state. It does some checking to see which way the player is facing but I am not sure if that’s important.

The enemies also have a new string called sub type. those enable me to separate the types of enemy fish there are. Right now there are two out of three in the game.

If a stage 2 fish is within the same distance, they will execute the Hunting state.

First, the idle function:


I check to see if the bool isSafe is true (this is used in the scared function to let the fishes know they are “safe” and can go back to being Idle again, you will see in a short while).
Then in given intervals I give the fishes random directions, the timer variable is updated in the enemy fish’s Update function. (you’ll see).

And that’s all it does.

for the enemy fishes, this will be interrupted by a change of AI state. Two functions will follow:


As you can see, when they are safe they will return to Idle state.


The scared function is very simple. The same distance vector as in the idle state is now made a unit vector pointing from enemy fish to the player and the enemy’s velocity is the inverted (the negative) unit vector times its speed times 5. This means it will go the exact opposite direction from the player fish 5 times faster than in their idle state. They also change direction so their faces are pointing away from the player fish. And if they are 500 pixels from the player fish they are “safe” and returns to the Idle state.


The hunting function is almost identical only the fishes move towards the player instead of away from it and they never stop hunting. They also only move 3 times faster while hunting.

and here is the enemy fish’s update function:


Here is a pointer to the player, this is not optimal because that means you could change things in the player from the enemy update function. I did not actually do this, but I kept it in lack of a better idea and lack of time. I only use it to get the player’s position and the direction the player is facing. Here is the timer variable that control the idle state movement updated, the state machine is updated here and the collider and the fish’s light is updated here and that’s all that’s needed. Perhaps the enemy update function could be made even shorter.


2 thoughts on “Artifact 5, AI states Idle, Scared and Hunting.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s