Understanding Unity FSM
Working with Unity “Finite State Machine” aka FSM
Unity Finite State Machine is the basic idea is that a character is engaged in some particular kind of action at any given time. The actions available will depend on the type of gameplay but typical actions include things like idling, walking, running, jumping, etc. These actions are referred to as states, in the sense that the character is in a “state” where it is walking, idling or whatever.
TLDR: A FSM (Finite State Machine) is a very glorified switch statement where an player/Ai is swapping between different states of actions based on the triggered condition. Each switch case logic has a preset of conditions that must be met before they are able to trigger the action.
Unity FSM can be applied as a basic Ai within a game (such as enemy Ai that can react to the players actions in real time) aka. get to close to the enemy and the enemies will be alerted and getting ready for combat.
Challenge:
Creating an FSM using our own Ai Logic:
Using the previous setup we are going to add in a more robust Ai function to our moving sphere.
To start, we are going to use something called “Enums”. An Enum is a custom type that you can create to label a set of functions. Just like how we definite states within Animation and in FSM, an Enum is just like those. For example we can create Enums for walking, running, jumping and attacking, and each Enum function can contain its own information.
Using the same Ai script that we have used to make the red sphere move around from waypoint to waypoint, we are going to rework some of the functions to add in Enums.
To start we will have 4 states, Walking, Jumping, Attacking and Death. Next we need to create a private variable to identify the current Enum State.
If you save the script and switch back to Unity, you will see in the Inspector Ai script that there is now a new drop down menu of all the named states we just created.
Now that we have the basic Enum states down, it is time to add some logic. We will need to have a function which can let the Ai swap between different states. This can be accomplished by using a switch statement.
Inside the Update Method we are going to create a new Switch statement that is adjusting the current Ai State. This switch statement will be the core brain of our FSM.
As a small challenge I will be scripting a function that will showcase the swapping between Enums in the switch statement, which will cause the Ai Sphere to stop moving.
First I did some quick reorganizing of the my AI script, I created new methods to isolate different functions. I gave individual methods for Calculating the movement, one for Jumping, and one method for controlling the AI.
Then I plot those methods into the corresponding slots.
Now when I test out this new Jumping state in Unity, the Ai Sphere automatically stops because it has swapped to a Jumping State instead of a walking state.
In the next article we will finish up the Enum state by adding the Attacking State and having the Ai sphere stop at every waypoint and pause for 3 seconds before automatically swapping back to the walking state.