April
27
2015

Learn more about animation state machines and what you can use them for.

Animation state machines are Unity state machine behaviours that are added to animator states. They’re used in animator controllers, e.g. to react on state changes.

An animation state machine can be added to a state by selecting a state and clicking on Add Behaviour > AnimationStateMachineComponent.

You can learn more about machine components in general and their basic settings in this how-to.

Start Types

An animation state machine is started by state machine and state change callback functions.

State Machine Enter

Started when transitioning from another state machine’s state to the state’s state machine. It’s implemented in the component’s OnStateMachineEnter function.

Local Start Variables

This start type will automatically initialize the following local variables.

  • stateMachinePathHash (int)
    Stores the full path hash of the state machine.

Additional Start Conditions

The State Machine Enter start type can optionally check the state machine’s path hash the OnStateMachineEnter function was called with. This can be used to e.g. only start the machine when the path hash matches a defined value.

Example

This start type can be used to change an animator parameter when entering a state machine. Use a Play Mecanim Animation node to change parameters of an animator.

State Machine Exit

Started when transitioning from the state’s state machine to another state machine’s state. It’s implemented in the component’s OnStateMachineExit function.

Local Start Variables

This start type will automatically initialize the following local variables.

  • stateMachinePathHash (int)
    Stores the full path hash of the state machine.

Additional Start Conditions

The State Machine Exit start type can optionally check the state machine’s path hash the OnStateMachineExit function was called with. This can be used to e.g. only start the machine when the path hash matches a defined value.

Example

This start type can be used to change an animator parameter when leaving a state machine. Use a Play Mecanim Animation node to change parameters of an animator.

State Enter

Started when the state machine starts evaluating the state (i.e. when entering the state). It’s implemented in the component’s OnStateEnter function.

Local Start Variables

This start type will automatically initialize the following local variables.

  • layerIndex (int)
    Stores the state’s layer index.
  • fullPathHash (int)
    Stores the full path hash of the state.
  • shortNameHash (int)
    Stores the path hash of the state.
    This hash is generated using Animator.StringToHash and doesn’t include the parent layer’s name.
  • tagHash (int)
    Stores the hash of the state’s tag.
  • length (float)
    Stores the current duration of the state.
  • normalizedTime (float)
    Stores the normalized time of the state.
  • loop (bool)
    Stores if the state is looping or not.

Additional Start Conditions

The State Enter start type can optionally check the layer index the OnStateEnter function was called with. This can be used to e.g. only start the machine when called from a defined layer.

Example

This start type can be used to play an audio clip when entering a state. Use a Play Sound node to play an audio clip.

State Exit

Started when the state machine ends evaluating the state (i.e. when leaving the state). It’s implemented in the component’s OnStateExit function.

Local Start Variables

This start type will automatically initialize the following local variables.

  • layerIndex (int)
    Stores the state’s layer index.
  • fullPathHash (int)
    Stores the full path hash of the state.
  • shortNameHash (int)
    Stores the path hash of the state.
    This hash is generated using Animator.StringToHash and doesn’t include the parent layer’s name.
  • tagHash (int)
    Stores the hash of the state’s tag.
  • length (float)
    Stores the current duration of the state.
  • normalizedTime (float)
    Stores the normalized time of the state.
  • loop (bool)
    Stores if the state is looping or not.

Additional Start Conditions

The State Exit start type can optionally check the layer index the OnStateExit function was called with. This can be used to e.g. only start the machine when called from a defined layer.

Example

This start type can be used to play an audio clip when leaving a state. Use a Play Sound node to play an audio clip.

State IK

Started immediately before it updates the animator’s internal IK (inverse kinematics) system but after Animator IK, when the state is active and the state’s layer has an IK pass. It’s implemented in the component’s OnStateIK function.

Local Start Variables

This start type will automatically initialize the following local variables.

  • layerIndex (int)
    Stores the state’s layer index.
  • fullPathHash (int)
    Stores the full path hash of the state.
  • shortNameHash (int)
    Stores the path hash of the state.
    This hash is generated using Animator.StringToHash and doesn’t include the parent layer’s name.
  • tagHash (int)
    Stores the hash of the state’s tag.
  • length (float)
    Stores the current duration of the state.
  • normalizedTime (float)
    Stores the normalized time of the state.
  • loop (bool)
    Stores if the state is looping or not.

Additional Start Conditions

The State IK start type can optionally check the layer index the OnStateIK function was called with. This can be used to e.g. only start the machine when called from a defined layer.

Example

This start type can be used to set the positions of the IK goals and their respective weights. Use an Animator Weight node to set the position and weight IK goals.

State Move

Started each frame the state is active, after Animator Move. It’s implemented in the component’s OnStateMove function.

Local Start Variables

This start type will automatically initialize the following local variables.

  • layerIndex (int)
    Stores the state’s layer index.
  • fullPathHash (int)
    Stores the full path hash of the state.
  • shortNameHash (int)
    Stores the path hash of the state.
    This hash is generated using Animator.StringToHash and doesn’t include the parent layer’s name.
  • tagHash (int)
    Stores the hash of the state’s tag.
  • length (float)
    Stores the current duration of the state.
  • normalizedTime (float)
    Stores the normalized time of the state.
  • loop (bool)
    Stores if the state is looping or not.

Additional Start Conditions

The State Move start type can optionally check the layer index the OnStateMove function was called with. This can be used to e.g. only start the machine when called from a defined layer.

Example

This start type can be used to move a game object based on root motion. Use a Store Parameter node to store parameters from the animator controller into variables and use them in one of the various Movement nodes (e.g. a Move node) to move the game object.

State Update

Started each frame the state is active, except the first and last frame (i.e. when State Enter and State Exit are used). It’s implemented in the component’s OnStateUpdate function.

Local Start Variables

This start type will automatically initialize the following local variables.

  • layerIndex (int)
    Stores the state’s layer index.
  • fullPathHash (int)
    Stores the full path hash of the state.
  • shortNameHash (int)
    Stores the path hash of the state.
    This hash is generated using Animator.StringToHash and doesn’t include the parent layer’s name.
  • tagHash (int)
    Stores the hash of the state’s tag.
  • length (float)
    Stores the current duration of the state.
  • normalizedTime (float)
    Stores the normalized time of the state.
  • loop (bool)
    Stores if the state is looping or not.

Additional Start Conditions

The State Update start type can optionally check the layer index the OnStateUpdate function was called with. This can be used to e.g. only start the machine when called from a defined layer.

Example

This start type can be used to play an audio clip when while being in a state. Use a Play Sound node to play an audio clip – additionally, you can check the local float variable normalizedTime (passed as start variable) to check the current play position of the state and only play the sound at specific points.