Framework: Unity FSM Implementation.

Suleiman Abdullah
5 min readMay 13


In the first step, I will create an enum inside the AI script called AIState, with the state of Running, Hide, and Death.

Now we need to create an enum variable called currentState.

In this step, I will set currentState to running when this game object is enabled when the game starts, and when he reaches the destination.

In this step, I will position AI to the block then I will remove the model and use the empty object as a hide position.

Go to the Update then add the switch statement which has currentState as a condition with the case as a state Running, Hide, Death.

In this step I will create a bool variable called isAgentHiding, then I will remove the AI model which I use to place them near the block.

I will go to the AI script and create two variables, a list of transform and int variables called _hidingWaypoints and _randomHideWayPointIndex

Select AI, drag and drop hidingWaypoint to the inspector.

In this step, I will go to the AI movement method in the AI script, when we are at the beginning of the waypoint in the else statement, I will add the if condition to check if _isAgentHiding is true, else we keep moving toward the destination.

When the AI script is activated we need to generate Random Number, we do this using the OnEnable method.

Why do I generate a random number in OnEnable?

Because this method is called automatically every time game Object Activate, since we re-use the enemy using Object pooling, using the Start method will generate this random Index once the game start, so the enemy will hide in the same spot which is not what we want.

When the game starts we need to set isAgentHiding equal to true.

Now go back to the AImovement method, when isAgentHiding is true, we need to move AI to the HidingWaypoints using a random Index.

After moving AI to the hide position, when AI reaches the hide position we need to switch the state from Running to Hide state. We do use the if condition to check if the AI x- position is equal to the pillar x -position. And make sure currentState is switched from running state to hide state since in this logic AI will already be in the hide position.

Note: Why I use the x positions to compare them, is because the navmesh agent has a base offset option which is a bit confusing, I will need more time to adjust this setting to make sure AI is in the exact position as the hidden barrier.

Now we create a Method called Hide, here we need to stop ai from running, and after a while, we need to set isAgent hiding to false and resume agent, set the current state to running again. I will call the hide method in Hiding State.

Create Die method or Damage method, I will create an int variable and bool variables, calling them health and _isDead.

Now I will Implement Damage logic, I won't explain I have covered this feature in my previous article.

Note: Every time we kill an enemy we need to grant the player 50 points. But this logic will be covered in the coming article too.

I will put the Damage method in Death State.

Last I will Activate Death State I will create a public method, which I will call in the player script. And this one will be in the coming article.

Let's the result from here.

See you in the next one.



Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development