Overview
Tested with: Unity 2022.3 LTS / Unity 6
"I want to add multiple animations to a character..." "I want to smoothly transition from walking to jumping..."
These are common challenges when implementing animations for characters and objects in Unity. The Animator Controller is Unity's official animation management system. It lets you visually manage multiple animation clips and control transitions between them using a State Machine.
Creating an Animator Controller
Method 1: Create Manually
- Right-click in the Project window
- Select "Create" > "Animator Controller"
- Set the file name (e.g., "PlayerAnimator")
Method 2: Automatic Creation
When you animate a GameObject using the Animation Window, an Animator Controller is automatically created.
Assigning to a GameObject
- Select the GameObject
- In the Inspector, select "Add Component" > "Animator"
- Drag and drop the Animator Controller you created into the Controller field of the Animator component
About Apply Root Motion
The Animator component has an "Apply Root Motion" checkbox.
| Setting | Behavior |
|---|---|
| On | Movement and rotation from animations are applied to the character |
| Off | Animation movement and rotation are ignored; controlled by script |
Tip for beginners: If you control character movement through scripts, set this to Off. Leaving it on can cause unintended behavior when both animation and script movement are applied simultaneously.
State Machine Basics
When you open the Animator Window, several default States are already in place.
| State | Description |
|---|---|
| Entry | The starting point for animation transitions |
| Any State | A special state that allows transitions to any state from any state |
| Exit | Returns to the parent layer from a Sub-State Machine (in the root layer, transitions back to the default State via Entry) |
About Exit behavior: Exit returns control to the parent State Machine when used inside a Sub-State Machine. In the root-level State Machine, transitioning to Exit loops back to the default State through Entry.
Creating a State
- Right-click on an empty area in the Animator Window
- Select "Create State" > "Empty"
- Assign an animation clip to the Motion field
Alternatively, you can drag and drop an animation clip from the Project window to automatically create a State.
Creating Transitions
- Right-click on the source State
- Select "Make Transition"
- Click on the destination State
Transition Settings
| Setting | Description |
|---|---|
| Has Exit Time | On: transitions after the animation finishes; Off: transitions immediately |
| Transition Duration | Duration of the transition (smoothness of the blend) |
| Conditions | Conditions that trigger the transition |
Using Parameters
Parameters are used for communication between scripts and the Animator Controller.
Parameter Types
| Type | Use Case |
|---|---|
| Float | Floating-point numbers (e.g., movement speed) |
| Int | Integers (e.g., ammo count) |
| Bool | True/false values (e.g., whether the character is walking) |
| Trigger | One-shot flags (e.g., jump) |
Creating Parameters
Click the "Parameters" tab on the left side of the Animator Window and add parameters using the "+" button.
Controlling the Animator from Script
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
void Update()
{
// Get movement input
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontal, 0, vertical);
float speed = movement.magnitude;
// Set the Animator parameter
animator.SetFloat("Speed", speed);
// Jump input
if (Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("Jump");
}
}
}
Parameter Setting Methods
animator.SetBool("isWalking", true);
animator.SetFloat("Speed", 5.0f);
animator.SetInteger("AmmoCount", 10);
animator.SetTrigger("Jump");
Using Blend Trees
A Blend Tree smoothly blends multiple animations based on parameter values.
Creating a Blend Tree
- Right-click on an empty area in the Animator Window
- Select "Create State" > "From New Blend Tree"
- Double-click the Blend Tree to open the editing view
- Select the "Blend Type" (typically "1D")
- Choose a "Parameter" (e.g., "Speed")
- Add animation clips and set their Thresholds
Example:
- Idle - Threshold: 0
- Walk - Threshold: 0.5
- Run - Threshold: 1.0
Using Animation Layers
Animation Layers allow you to overlay multiple State Machines.
For example, you can play a full-body walking animation while playing an attack animation on the upper body only.
Layer Settings
| Setting | Description |
|---|---|
| Weight | Layer weight (0 to 1) |
| Blending | Override (replace) or Additive (add on top) |
| Mask | Which body parts the layer applies to (Avatar Mask) |
Practical Examples
Example 1: Movement Animation (Blend Tree + Script)
using UnityEngine;
public class MovementAnimator : MonoBehaviour
{
[SerializeField] private float walkSpeed = 2f;
[SerializeField] private float runSpeed = 5f;
private Animator animator;
private CharacterController controller;
// Hash parameter names for better performance
private static readonly int SpeedHash = Animator.StringToHash("Speed");
void Start()
{
animator = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
}
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 move = new Vector3(h, 0, v);
// Sprint check (Shift key)
float targetSpeed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
// Normalize to match Blend Tree threshold (0 to 1)
// move.magnitude can be sqrt(2) (~1.41) during diagonal movement, so clamp it
float normalizedSpeed = Mathf.Clamp01(move.magnitude) * (targetSpeed / runSpeed);
animator.SetFloat(SpeedHash, normalizedSpeed);
// Movement
controller.Move(move.normalized * targetSpeed * Time.deltaTime);
}
}
Performance tip: Using
Animator.StringToHashto hash parameter names avoids per-frame string comparisons. This is especially effective for frequently called methods likeSetFloatandSetBool.
Example 2: Jump Animation
// Any State to Jump Transition settings:
// - Conditions: Jump (Trigger)
// - Has Exit Time: Off
// - Transition Duration: 0.1
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
animator.SetTrigger("Jump");
}
}
Example 3: Attack Animation (Combo Support)
// Transition settings:
// - Idle to Attack: Conditions: Attack (Trigger), Has Exit Time: Off
// - Attack to Idle: Has Exit Time: On (auto-transition after attack animation finishes)
void Update()
{
if (Input.GetMouseButtonDown(0))
{
animator.SetTrigger("Attack");
}
}
Animator Override Controller
The Animator Override Controller lets you reuse the structure (State Machine, Transitions) of an existing Animator Controller while swapping out animation clips.
Use Cases
- Enemy variations (same behavior, different visuals)
- Swapping attack motions per weapon type
- Character skin variants
How to Create
- Right-click in the Project window > Create > Animator Override Controller
- Set the source Animator Controller in the "Controller" field
- Replace the animation clips shown in the list
// Dynamically swapping via script
public AnimatorOverrideController slimeOverride;
public AnimatorOverrideController goblinOverride;
void SetEnemyType(EnemyType type)
{
animator.runtimeAnimatorController = type == EnemyType.Slime
? slimeOverride
: goblinOverride;
}
Summary
The Animator Controller is Unity's official animation management system.
- State Machine - Visually manage multiple animations
- Parameters - Control animations from scripts
- Transitions - Configure transitions between animations
- Blend Tree - Smoothly blend multiple animations
- Animation Layers - Overlay multiple animations
Use the Animator Controller to bring your characters to life with dynamic, responsive animations.