Animator, Blend Trees & Timeline
Characters in Unity are animated by an Animator component running an Animator Controller: a state machine whose states play animation clips and whose transitions are driven by parameters that your code sets. Timeline is the separate tool for authored sequences: cutscenes, intros, scripted events.
The Animator Controller
- Import a rigged character (FBX). In its Rig tab choose Humanoid to retarget animations between characters, or Generic for non-humanoids.
- Create > Animator Controller, assign it to the character's Animator, and open Window > Animation > Animator.
- Add parameters (Float
Speed, BoolGrounded, TriggerJump, TriggerAttack). - Create states (drag clips in), then transitions between them with conditions (
Grounded == false,Jumptrigger). Untick Has Exit Time for transitions that should react immediately to input. - Use layers with Avatar Masks for upper-body actions (shooting while running), and Any State transitions for things like "hit" or "die" that can interrupt anything.
using UnityEngine;
public class CharacterAnimation : MonoBehaviour
{
static readonly int Speed = Animator.StringToHash("Speed"); // hash once: no string lookup per frame
static readonly int Grounded = Animator.StringToHash("Grounded");
static readonly int Jump = Animator.StringToHash("Jump");
[SerializeField] Animator animator;
[SerializeField] CharacterController controller;
void Update()
{
Vector3 v = controller.velocity;
float planarSpeed = new Vector2(v.x, v.z).magnitude;
animator.SetFloat(Speed, planarSpeed, 0.1f, Time.deltaTime); // damped: smooth blend changes
animator.SetBool(Grounded, controller.isGrounded);
}
public void PlayJump() => animator.SetTrigger(Jump);
// Animation events: add an event on a clip's timeline that calls a public method like this.
public void OnFootstep() { /* play a footstep sound */ }
}Root motion: with Apply Root Motion on, the animation itself moves the character (the clip's root bone movement drives the transform). It gives perfectly planted feet but less responsive control. Gameplay-first games usually move the character in code and keep root motion off, or use OnAnimatorMove to combine both.
Blend trees
A blend tree is a state that mixes several clips by a parameter. A 1D tree on Speed with idle at 0, walk at 2 and run at 6 m/s plays a weighted mix of the two clips around the current speed. The model computes those weights the same way Unity's 1D blend tree does (linear between neighbouring thresholds):
using System;
var motions = new (string Clip, double Threshold)[] { ("Idle", 0), ("Walk", 2), ("Run", 6) };
foreach (double speed in new[] { 0.0, 1.0, 2.0, 3.0, 5.0, 6.0, 8.0 })
{
double[] w = Weights(motions, speed);
Console.Write(quot;speed {speed,3:F1}:");
for (int i = 0; i < motions.Length; i++)
if (w[i] > 0) Console.Write(quot; {motions[i].Clip} {w[i] * 100:F0}%");
Console.WriteLine();
}
static double[] Weights((string Clip, double Threshold)[] m, double p)
{
var w = new double[m.Length];
if (p <= m[0].Threshold) { w[0] = 1; return w; }
if (p >= m[^1].Threshold) { w[^1] = 1; return w; } // clamp above the last threshold
for (int i = 0; i < m.Length - 1; i++)
if (p >= m[i].Threshold && p <= m[i + 1].Threshold)
{
double t = (p - m[i].Threshold) / (m[i + 1].Threshold - m[i].Threshold);
w[i] = 1 - t; w[i + 1] = t;
return w;
}
return w;
}speed 0.0: Idle 100%
speed 1.0: Idle 50% Walk 50%
speed 2.0: Walk 100%
speed 3.0: Walk 75% Run 25%
speed 5.0: Walk 25% Run 75%
speed 6.0: Run 100%
speed 8.0: Run 100%
Thresholds should match the real speed of each clip (the speed at which the feet don't slide). Use Compute Thresholds > Speed in the blend tree Inspector to get them from root motion, or measure them. Mismatched thresholds are the usual cause of "ice skating" feet. 2D blend trees (Freeform Directional) do the same for strafing with MoveX/MoveY parameters.
Timeline
Timeline (com.unity.timeline, included in the templates) arranges tracks on a time ruler: Animation tracks, Activation tracks (turn objects on/off), Audio tracks, Signal tracks (call methods at a time) and Cinemachine tracks (cut and blend between cameras).
- Select a GameObject and open Window > Sequencing > Timeline, then click Create to make a Timeline asset and a Playable Director component.
- Drag objects into the track list to bind them, add clips, and scrub.
- Play it from code:
using UnityEngine;
using UnityEngine.Playables;
public class Cutscene : MonoBehaviour
{
[SerializeField] PlayableDirector director;
public void PlayIntro()
{
director.stopped += OnFinished;
director.Play();
}
void OnFinished(PlayableDirector d)
{
d.stopped -= OnFinished;
// hand control back to the player
}
}Tip: The Animator is relatively expensive per character. For hundreds of simple animated objects (spinning coins, swaying grass), animate them in a shader or with a simple script instead, and set Culling Mode to Cull Update Transforms or Cull Completely on Animators that may be off screen.
Test yourself
A 1D blend tree has Walk at 2 and Run at 6. The Speed parameter is 4. What plays?
- Run 100%
- Walk 50% and Run 50%
- Walk 100%
- Walk 25% and Run 75%
Why use Animator.StringToHash("Speed") stored in a static field?
- It is required by Mecanim
- It avoids hashing the parameter name on every call
- It makes the parameter public
- It enables root motion
Which tool is meant for authored cutscenes with camera cuts, audio and animation on a time ruler?
- Animator Controller
- Timeline with a Playable Director
- Blend trees
- Avatar Masks