Animator
TypeClass (Component)
NamespaceUnityEngine
InheritsBehaviour > Component > Object
NeedsAn Animator Controller asset

Animator

The Animator component plays animations using an Animator Controller: a flowchart of animation states (Idle, Run, Jump) and the arrows between them. Your code doesn't usually play animations directly. It sets parameters, and the flowchart decides which animation to play.

The Animator Controller is a train track map. Each state is a station (Idle, Walk, Run) and each transition is a track switch with a rule, like "switch to Run when Speed is over 3". Your script doesn't drive the train. It just updates the signal values (SetFloat("Speed", 5)), and the train follows the track on its own.

Parameter types

Create parameters in the Animator window's Parameters tab, then use them as conditions on transitions.

TypeSet withUse for
FloatSetFloatSpeed, blend trees (walk → run)
IntSetIntegerWeapon type, combo step
BoolSetBoolStates that last: IsGrounded, IsCrouching
TriggerSetTriggerOne-off events: Jump, Attack, Hit. Resets itself after being used.
Parameters: Speed (float), Jump (trigger)EntryIdleRunJumpAny StateSpeed > 0.1Speed < 0.1Jump triggerclip finishes (Has Exit Time)
A typical Animator Controller. Your script only sets Speed and fires Jump; the highlighted transitions check those parameters and the Animator changes state on its own.

Properties

NameTypeDescription
speedfloatPlayback speed multiplier. 1 = normal.
runtimeAnimatorControllerRuntimeAnimatorControllerThe controller in use. Can be swapped at runtime.
applyRootMotionboolIf true, the animation itself moves the character (e.g. walking clips that move forward).
updateModeAnimatorUpdateModeNormal, Fixed (in sync with physics), or Unscaled Time (plays when paused).

Methods

MethodReturnsDescription
SetFloat(string name, float value)voidSet a float parameter.
SetBool(string name, bool value)voidSet a bool parameter.
SetInteger(string name, int value)voidSet an int parameter.
SetTrigger(string name)voidFire a trigger.
ResetTrigger(string name)voidCancel a trigger that hasn't been used yet.
GetFloat, GetBool, GetIntegervariesRead parameters.
Play(string stateName)voidJump straight to a state, skipping transitions.
CrossFade(string stateName, float duration)voidBlend smoothly into a state over duration (normalized time).
GetCurrentAnimatorStateInfo(int layer)AnimatorStateInfoInfo about the playing state: IsName("Attack"), normalizedTime (0 = start, 1 = end of first loop).
Animator.StringToHash(string name)intStatic. Turns a name into an ID. Faster than passing strings every frame.

Code Samples

Driving a character's animations

using UnityEngine;

public class PlayerAnimation : MonoBehaviour
{
    // Hash once, reuse forever (faster than strings, and typos show up in one place).
    private static readonly int SpeedHash    = Animator.StringToHash("Speed");
    private static readonly int GroundedHash = Animator.StringToHash("IsGrounded");
    private static readonly int JumpHash     = Animator.StringToHash("Jump");

    [SerializeField] private Animator animator;
    [SerializeField] private Rigidbody rb;

    void Update()
    {
        Vector3 flatVelocity = new Vector3(rb.linearVelocity.x, 0f, rb.linearVelocity.z);
        animator.SetFloat(SpeedHash, flatVelocity.magnitude);
        animator.SetBool(GroundedHash, IsGrounded());

        if (Input.GetButtonDown("Jump"))
        {
            animator.SetTrigger(JumpHash);
        }
    }

    bool IsGrounded() => Physics.Raycast(transform.position + Vector3.up * 0.1f, Vector3.down, 0.2f);
}

In the Animator window, you'd then add transitions such as: Idle → Run when Speed > 0.1, Run → Idle when Speed < 0.1, Any State → Jump when Jump is triggered.

Animation Events: calling code from an animation

In the Animation window, you can add an event at a specific frame (e.g. the frame the sword connects). Unity then calls a public method with that name on a script on the same GameObject as the Animator:

// Called by an Animation Event on the "Attack" clip.
public void OnSwordHit()
{
    Debug.Log("Deal damage now");
}

Wait until an animation finishes

IEnumerator PlayDeath()
{
    animator.Play("Death");
    yield return null; // let the Animator switch state first
    while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1f)
        yield return null;
    Destroy(gameObject);
}

What happens at runtime

  1. Your Update sets parameters. Nothing visible happens yet.
  2. After all Update calls, the Animator evaluates the current state's transitions. If a condition is met, it starts blending to the next state.
  3. It samples the animation clips and writes the resulting positions/rotations into the bone Transforms.
  4. LateUpdate runs after this, so it's where you'd override a bone (e.g. make the head look at something).
  5. A trigger stays "set" until a transition uses it. If nothing uses it, it stays on and fires later when you don't expect it. Use ResetTrigger if needed.

Common Mistakes

See also