Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 36 ยท Animation and Audio

The Animator Controller in Unity

Build character animation logic with the Unity Animator Controller - states, transitions, parameters, blend trees and layers - with a runnable state machine stand-in.

A character that only slides across the floor feels dead. To make it idle, walk, run, jump and fall, Unity uses the Animator. You give a game object an Animator component, assign an Animator Controller (a state machine asset) and then drive it from your scripts with a few parameters.

This lesson explains the pieces: states, transitions, parameters, blend trees and layers. You do not need to write animation clips yourself; clips come from an artist, the Asset Store, or the next lesson. Because Unity is not available here, the C# that runs on this page is a stand-in state machine that behaves like an Animator, and the Unity code is read-only.

The Animator in four parts

  1. The Animator component sits on the game object (the model or sprite). It has a Controller, an optional Avatar (for humanoid rigs) and a few options such as Apply Root Motion.
  2. The Animator Controller is an asset edited in the Animator window (Window > Animation > Animator). It is a graph of states.
  3. Each State plays a motion: a single Animation Clip or a Blend Tree.
  4. Transitions are arrows between states with conditions based on Parameters.
stateDiagram-v2
  [*] --> Idle
  Idle --> Run: Speed > 0.1
  Run --> Idle: Speed < 0.1
  Idle --> Jump: Jump trigger
  Run --> Jump: Jump trigger
  Jump --> Idle: Exit Time (clip ended)

A small locomotion graph; Idle is the default state.

Parameters

Parameters are the variables scripts set. There are four types:

Type Use it for Set with
Float speed, blend amounts SetFloat("Speed", v)
Int weapon type, combo step SetInteger("Weapon", 2)
Bool grounded, crouching SetBool("Grounded", true)
Trigger one-shot events (jump, attack) SetTrigger("Jump")

A Trigger switches on until a transition consumes it, then resets automatically. That is what you want for "the player pressed jump" because you cannot miss it if the animator is busy for a frame.

C#
using UnityEngine;

[RequireComponent(typeof(Animator))]
public class PlayerAnimation : MonoBehaviour
{
    static readonly int SpeedHash = Animator.StringToHash("Speed");
    static readonly int GroundedHash = Animator.StringToHash("Grounded");
    static readonly int JumpHash = Animator.StringToHash("Jump");

    Animator animator;

    void Awake() => animator = GetComponent<Animator>();

    public void Tick(float speed, bool grounded)
    {
        animator.SetFloat(SpeedHash, speed, 0.1f, Time.deltaTime); // damped
        animator.SetBool(GroundedHash, grounded);
    }

    public void Jump() => animator.SetTrigger(JumpHash);
}

Use Animator.StringToHash once and cache the integer: passing the string each frame hashes it every call. The four-argument SetFloat smooths the value so blends do not pop.

Transitions in detail

A transition has several fields worth understanding:

  • Has Exit Time: the transition waits until the source clip reaches a normalized time (for example 0.9 = 90%). Good for "play the whole attack, then go back to idle". Turn it off for responsive transitions like run to idle.
  • Transition Duration: how long the two poses blend. Short (0.05 to 0.15 s) for snappy actions, longer for locomotion.
  • Conditions: all of them must be true. Several transitions leave the same state; the first one whose conditions pass wins.
  • Any State: a special node whose transitions can fire from anywhere, handy for "hit reaction" and "die".
  • Interruption Source: whether a transition can be interrupted by another one while blending.

A common beginner problem: the animation seems stuck or delayed because Has Exit Time is still on for a transition that should be instant.

Blend trees

Instead of separate Walk and Run states, a 1D Blend Tree blends several clips along one parameter. At Speed = 0 it plays Idle, at 0.5 a mix of Idle and Walk, at 1 Run. A 2D Freeform blend tree uses two parameters (for example MoveX, MoveY) for strafing in eight directions. The blend result is a weighted average of the clips whose thresholds surround the parameter value.

C# Stand-in: 1D blend tree weights

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Layers and avatar masks

An Animator Controller can have several layers. Layer 0 usually handles the whole body (locomotion); a second layer with an Avatar Mask that only includes the arms can play "reload" or "wave" on top while the legs keep running. Layers blend with a Weight (settable from code with SetLayerWeight) and can be Override or Additive. Remember that a layer's weight 0 means it does nothing.

A state machine you can run

To understand what Unity does every frame, here is a stripped-down animator: states, parameters, transition conditions, a trigger that is consumed, and exit time. Try changing the inputs.

C# Stand-in: a tiny Animator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Notice that the trigger is set in one frame and consumed by the transition, while the "Jump to Idle" transition waits for StateTime (exit time).

Debugging tips

  • Open the Animator window while the game runs; the active state shows a progress bar and transitions glow.
  • A parameter name typo silently does nothing in older code; the hash approach plus a constants class reduces this.
  • If an animation plays but the character does not move, check Apply Root Motion: when on, the clip moves the transform, and your script's movement fights it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which parameter type resets itself after a transition uses it?

    A Trigger stays set until a transition consumes it, then it is cleared automatically.
  2. 2. You want run-to-idle to feel instant. Which transition setting should you turn off?

    With exit time on, the transition waits for the source clip to reach the exit time before it can fire.
  3. 3. What does a 1D Blend Tree do?

    It interpolates the weights of clips at neighboring thresholds.
  4. 4. Why call Animator.StringToHash once and cache the result?

    Using the cached int overload is faster than passing a string each frame.

Summary

  • Animator component + Animator Controller asset + states, transitions and parameters.
  • Parameters (Float, Int, Bool, Trigger) are set from scripts; cache their hashes.
  • Exit Time and Duration decide how transitions feel; Any State handles interrupts.
  • Blend trees mix clips by a parameter; layers with avatar masks play actions on part of the body.

Next: Animation Clips and Animation Events