Finite State Machines

Animancer includes a general purpose FSM system which is flexible enough for most needs, but it is not tied to the animation system in any way so you can use either one without the other if you want to.

The full source code of the Animancer.FSM system is included with both Animancer Lite and Pro (in the Assets/Plugins/Animancer/Utilities/FSM folder).

Basic Usage

  1. Make a base state class which implements the IState Interface (or Inherits from something that implements it such as State or StateBehaviour).
  2. Make a StateMachine<TState> field (where TState is the class you just made).
  3. Initialize that field with a new StateMachine(startingState).
  4. Change the current state by calling stateMachine.TrySetState(nextState).
  5. Access the current state using its CurrentState property.

Here is a simple script which shows the system in action:

  1. Copy the code below into a new C# script called StateMachineExample.cs (to match the StateMachineExample class name).
  2. Drag that script onto an object in the scene.
  3. Enter Play Mode.
  4. Observe the messages it logs in the Console window.
  5. Click the mouse to change states.
using Animancer.FSM;
using UnityEngine;

public sealed class LoggerState : IState
{
    public readonly string Name;

    public LoggerState(string name)
    {
        Name = name;
    }

    public bool CanEnterState
    {
        get
        {
            Debug.Log("CanEnterState: " + Name);
            return true;
        }
    }

    public bool CanExitState
    {
        get
        {
            Debug.Log("CanExitState: " + Name);
            return true;
        }
    }

    public void OnEnterState()
    {
        Debug.Log("OnEnterState: " + Name);
    }

    public void OnExitState()
    {
        Debug.Log("OnExitState: " + Name);
    }
}

public sealed class StateMachineExample : MonoBehaviour
{
    private LoggerState _Idle = new LoggerState("Idle");
    private LoggerState _Walk = new LoggerState("Walk");
	
    private readonly StateMachine
        StateMachine = new StateMachine();

    private void Awake()
    {
        // Start in the Idle state.
		StateMachine.ForceSetState(_Idle);
    }

    private void Update()
    {
        // Swap between the two states whenever the mouse is clicked.
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Mouse was Clicked");
            
            if (_StateMachine.CurrentState == _Idle)
                _StateMachine.TrySetState(_Walk);
            else
                _StateMachine.TrySetState(_Idle);
        }
    }
}

The State Machines examples demonstrate how to use it in more detail and the 3D Game Kit example demonstrates how to completely redesign an Animator Controller based character to use this system instead (as well as solve some of the issues with the original implementation).

Overview A summary of the system structure and the reasons it was designed that way.
Changing States How to change the current state of a state machine.
State Types Common base state types.
Keys Keyless and keyed state machines.
Utilities Other utilities included in the system.