Introduction to the Unity State Machine Pattern

Murat Han Acıpayam
4 min readJul 18, 2023

In game development, managing the various states and behaviors of characters, objects, or systems is crucial for creating dynamic and interactive experiences. One popular design pattern used for this purpose is the State Machine pattern. Unity, a powerful game engine, provides various tools and techniques to implement state machines effectively. In this article, we will explore the Unity State Machine pattern in detail, along with code examples.

Understanding State Machines

Before diving into the implementation, let’s briefly discuss the concept of state machines. A state machine is a mathematical model used to describe the behavior of a system. It consists of a set of states, transitions, and actions. A state represents a specific behavior or condition of the system, while a transition defines the movement from one state to another. Actions are associated with states or transitions and represent the behavior to be performed when entering, exiting, or during a state.

Implementing State Machines in Unity

Unity offers several approaches to implement state machines, such as using scripts, visual tools like Animator Controller, or third-party plugins. In this article, we’ll focus on a script-based approach, which provides flexibility and control over the state machine logic.

Step 1: Defining the States

The first step in implementing a Unity state machine is to define the states. Each state will be represented by a separate script. Let’s consider an example where we have a character with three states: Idle, Walk, and Run. We’ll create three state scripts accordingly.

// IdleState.cs
public class IdleState : State
{
public override void Enter()
{
// Enter idle state logic
}

public override void Update()
{
// Idle state update logic
}

public override void Exit()
{
// Exit idle state logic
}
}

// WalkState.cs
public class WalkState : State
{
public override void Enter()
{
// Enter walk state logic
}

public override void Update()
{
// Walk state update logic
}

public override void Exit()
{
// Exit walk state logic
}
}

// RunState.cs
public class RunState : State
{
public override void Enter()
{
// Enter run state logic
}

public override void Update()
{
// Run state update logic
}

public override void Exit()
{
// Exit run state logic
}
}

In the above example, each state script extends a base State class, which is an abstract class. The Enter, Update, and Exit methods represent the actions to be performed when entering, updating, or exiting a state, respectively.

Step 2: Creating the State Machine Controller

Next, we need a state machine controller script to manage the state transitions and execute the appropriate actions. Let’s create a StateMachineController script for this purpose.

// StateMachineController.cs
public class StateMachineController : MonoBehaviour
{
private State currentState;

private void Start()
{
// Initialize the state machine with the initial state
currentState = new IdleState();
currentState.Enter();
}

private void Update()
{
// Update the current state
currentState.Update();
}

public void ChangeState(State newState)
{
// Exit the current state
currentState.Exit();

// Assign the new state and enter it
currentState = newState;
currentState.Enter();
}
}

The StateMachineController script initializes the state machine with an initial state (in this case, IdleState) and updates the current state in the Update method. The ChangeState method is used to transition between states. It first exits the current state, assigns the new state, and then enters the new state.

Step 3: Triggering State Transitions

Now that we have the state scripts and the state machine controller, we can trigger state transitions based on specific conditions or events. Let’s assume we want to transition from the IdleState to the WalkState when the player presses a movement key.

// PlayerController.cs
public class PlayerController : MonoBehaviour
{
private StateMachineController stateMachine;

private void Start()
{
stateMachine = GetComponent<StateMachineController>();
}

private void Update()
{
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) ||
Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D))
{
stateMachine.ChangeState(new WalkState());
}
}
}

In the above example, the PlayerController script retrieves the StateMachineController component attached to the player character. When the player presses a movement key (e.g., W, A, S, D), it triggers a state transition from IdleState to WalkState.

Step 4: Extending the State Machine

You can extend the state machine by adding more states and defining additional transitions between them. For example, we can add a transition from WalkState to RunState when the player presses the Shift key while in the WalkState.

// PlayerController.cs
public class PlayerController : MonoBehaviour
{
// ...

private void Update()
{
// ...

if (Input.GetKeyDown(KeyCode.LeftShift) && stateMachine.GetCurrentState() is WalkState)
{
stateMachine.ChangeState(new RunState());
}
}
}

In this extended example, when the player is in the WalkState, pressing the Left Shift key triggers a state transition from WalkState to RunState.

Conclusion

The Unity State Machine pattern is a powerful tool for managing complex behaviors and interactions in games. By using a script-based approach, you have full control over the state machine’s logic and can easily extend it as needed. In this article, we explored the implementation of a basic state machine in Unity, defining states, creating a state machine controller, triggering state transitions, and extending the state machine with additional states and transitions. With these concepts and code examples, you can start implementing and leveraging state machines in your own Unity projects.

--

--