Finite State Machines In Unity

A finite-state machine (FSM) is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some inputs; the change from one state to another is called a transition. An FSM is defined by a list of its states, its initial state, and the inputs that trigger each transition.

Simply Put you have a finite number of states that you can be in and you want to transition between them.

FSM’s are useful in a lot of situations, Animations, AI, or Locomotion systems just to name a few.


I am going to Take a look at a Locomotion State.

Pure Code.

To define what state I am in I will be using an Enum.

enum state
Current state

Now I have to take care of the actions.

Jump action
Fall Action
Land Action
Crouch Action

Now I have to take care calling these methods from the Update Method.

Fake Gravity, Pressing the Jump or Crouch Buttons.

Using Animator Component.

Creating Finite State Machines in Code is not very clean as it can be difficult to follow the transitions from one state to another. Unity has one built in designed for animations but it also works great for creating a FSM.

The Locomotion FSM can be simplified to just a variable to cache the animator component and an Update Method to pass on the Variables that are used to control the State.

Now on the Unity Side I create an Animator Controller for the FSM and add it to the Game Object.

Added Animator to FSM in Unity.

I Edit the Animator Controller and add the States to it.

Adding States to the Controller

I added the Bool Parameters to use for transitioning the States.

Bool Parameters to control The States.

I set the transition up in the Animator.

Now It is extremely easy to follow the code and see exactly what is going on. We can even watch it change states in the Animator. It changes states like it is suppose to / Even though my fake gravity causes it to almost instantly return to the grounded state, if you blink you miss it going into the In Air State.

The Code Can be Found on my Git Hub Page




Excited about changing my hobby into a new carer with GameDevHQ course.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Snowflake Single Sign-On Using Azure AD

Best solutions for Codility Lessons Lesson 1 Iterations

Key concepts about using JProfiler

What’s new in Elsa 2.0

Critical Path in Project Management — How to

Types of Software development

Running Argo Workflow in Azure Kubernetes Service

How to Make Two Cloud Platforms Even More Effective

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
James Lafritz

James Lafritz

Excited about changing my hobby into a new carer with GameDevHQ course.

More from Medium

The Observer Pattern In Unity

Making The Point With Primitives

Camera Shake in Unity

Dynamic Library In C