Unity Animation (Animator / Mecanim)
Control animation state with Unity 6.3 LTS's Animator and Animator Controllers: parameters,
transitions, blend trees, layers, and humanoid IK. Targets Unity 6.3 LTS (6000.3).
When to use
- Use when connecting animation clips into a state machine, driving them from script via
parameters, blending locomotion (idle→walk→run), layering an upper-body action over
movement, or adding foot/hand IK on a humanoid rig.
- Use when the project has
*.controller (Animator Controller) and *.anim assets, or a
rigged model with an Avatar.
When not to use: simple non-skeletal value tweens (UI fades, position lerps) are better
done with a tween/coroutine — see unity-csharp-scripting. Timeline cutscenes are a separate
tool. 2D sprite frame animation also uses the Animator but with sprite keyframes.
Core workflow
- Add an
Animator to the model and assign an Animator Controller; for a humanoid model,
set its rig to Humanoid so it has an Avatar (enables retargeting and IK).
- Define parameters on the controller —
Float (Speed), Bool (IsGrounded), Int,
Trigger (Jump) — and states with transitions whose conditions read those parameters.
- Set parameters from script, never poke states directly:
SetFloat, SetBool,
SetInteger, SetTrigger. The state machine resolves transitions for you.
- Blend continuous motion with a Blend Tree (one
Float like Speed drives idle↔walk↔run)
instead of many discrete states + transitions.
- Layer additive/override motion (e.g. an upper-body "aim" layer with an Avatar Mask) and
control its
layerWeight.
- Verify in the Animator window during Play mode — the live state highlights and parameter
values update, so you can see exactly which transition fired (or didn't).
Patterns
1. Drive locomotion + a one-shot action from script
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class CharacterAnim : MonoBehaviour
{
private Animator _anim;
// Cache parameter hashes — faster and typo-proof vs string lookups every frame.
private static readonly int Speed = Animator.StringToHash("Speed");
private static readonly int IsGrounded= Animator.StringToHash("IsGrounded");
private static readonly int Jump = Animator.StringToHash("Jump");
private void Awake() => _anim = GetComponent<Animator>();
public void Tick(float planarSpeed, bool grounded)
{
_anim.SetFloat(Speed, planarSpeed); // drives a 1D blend tree (idle/walk/run)
_anim.SetBool(IsGrounded, grounded); // gates a falling/landing transition
}
public void DoJump() => _anim.SetTrigger(Jump); // fire-and-forget; auto-resets after use
}
2. Smooth a noisy input into a blend parameter
// dampTime smooths Speed so the blend tree doesn't snap; great for analog sticks.
_anim.SetFloat(Speed, targetSpeed, 0.1f /* dampTime */, Time.deltaTime);
3. Play / cross-fade a state directly (bypassing parameter conditions)
// Useful for hit reactions where you want an immediate, explicit transition.
_anim.CrossFade("Hit", 0.1f); // blend over 0.1s normalized
// Or jump instantly: _anim.Play("Hit");
4. Wait until the current state finishes
private System.Collections.IEnumerator AfterAttack()
{
var info = _anim.GetCurrentAnimatorStateInfo(0); // layer 0
yield return new WaitForSeconds(info.length); // approximate clip length
// ...follow-up logic
}
Pitfalls
SetTrigger missed or "sticks" — triggers are consumed by the next satisfied transition
and auto-reset; if no transition consumes it, it can fire later unexpectedly. Use
ResetTrigger to clear, or prefer a Bool when the condition is a sustained state.
- String parameter typos fail silently — a misspelled name just does nothing. Use
Animator.StringToHash and cache the int hashes.
- Transition feels laggy —
Has Exit Time makes the transition wait for the clip to reach
a normalized time. Uncheck it for responsive, condition-driven transitions (jump, hit).
- Character slides or won't move —
Apply Root Motion is on but your code also moves the
transform (or vice versa). Decide: root motion or scripted movement, not both.
- Upper-body layer overrides the whole body — set the layer's Blend mode (Override vs
Additive), assign an Avatar Mask, and tune
layerWeight (0–1).
- IK does nothing — IK only applies inside
OnAnimatorIK, requires "IK Pass" enabled on
the layer, and needs a Humanoid Avatar.
References
- For blend trees (1D vs 2D Freeform/Directional), animation layers + Avatar Masks,
and humanoid IK (
OnAnimatorIK, SetIKPositionWeight, SetIKPosition, look-at), read
references/blend-trees-and-ik.md.
- Primary docs: Unity Manual "Animation" section and
ScriptReference/Animator.
Related skills
unity-csharp-scripting — the MonoBehaviour and coroutine timing used above.
unity-physics — moving the body that the animation visualises.
game-ai — deciding when to play which animation state.
1---
2name: unity-animation
3description: >
4 Drive Unity 6.3 LTS character animation with Animator Controllers: states, transitions,
5 parameters, blend trees, animation layers, and humanoid Avatar IK. Use when wiring an
6 Animator, setting parameters from script (SetFloat/SetBool/SetTrigger), building blend
7 trees, or when the user mentions Animator, Mecanim, state machine, blend tree, or .controller.
8---
9
10# Unity Animation (Animator / Mecanim)
11
12Control animation state with Unity 6.3 LTS's `Animator` and Animator Controllers: parameters,
13transitions, blend trees, layers, and humanoid IK. Targets **Unity 6.3 LTS (6000.3)**.
14
15## When to use
16
17- Use when connecting animation clips into a state machine, driving them from script via
18 parameters, blending locomotion (idle→walk→run), layering an upper-body action over
19 movement, or adding foot/hand IK on a humanoid rig.
20- Use when the project has `*.controller` (Animator Controller) and `*.anim` assets, or a
21 rigged model with an Avatar.
22
23**When *not* to use:** simple non-skeletal value tweens (UI fades, position lerps) are better
24done with a tween/coroutine — see `unity-csharp-scripting`. Timeline cutscenes are a separate
25tool. 2D sprite frame animation also uses the Animator but with sprite keyframes.
26
27## Core workflow
28
291. **Add an `Animator`** to the model and assign an Animator Controller; for a humanoid model,
30 set its rig to **Humanoid** so it has an Avatar (enables retargeting and IK).
312. **Define parameters** on the controller — `Float` (Speed), `Bool` (IsGrounded), `Int`,
32 `Trigger` (Jump) — and states with transitions whose *conditions* read those parameters.
333. **Set parameters from script**, never poke states directly: `SetFloat`, `SetBool`,
34 `SetInteger`, `SetTrigger`. The state machine resolves transitions for you.
354. **Blend continuous motion with a Blend Tree** (one `Float` like Speed drives idle↔walk↔run)
36 instead of many discrete states + transitions.
375. **Layer additive/override motion** (e.g. an upper-body "aim" layer with an Avatar Mask) and
38 control its `layerWeight`.
396. **Verify** in the Animator window during Play mode — the live state highlights and parameter
40 values update, so you can see exactly which transition fired (or didn't).
41
42## Patterns
43
44### 1. Drive locomotion + a one-shot action from script
45
46```csharp
47using UnityEngine;
48
49[RequireComponent(typeof(Animator))]
50public class CharacterAnim : MonoBehaviour
51{
52 private Animator _anim;
53 // Cache parameter hashes — faster and typo-proof vs string lookups every frame.
54 private static readonly int Speed = Animator.StringToHash("Speed");
55 private static readonly int IsGrounded= Animator.StringToHash("IsGrounded");
56 private static readonly int Jump = Animator.StringToHash("Jump");
57
58 private void Awake() => _anim = GetComponent<Animator>();
59
60 public void Tick(float planarSpeed, bool grounded)
61 {
62 _anim.SetFloat(Speed, planarSpeed); // drives a 1D blend tree (idle/walk/run)
63 _anim.SetBool(IsGrounded, grounded); // gates a falling/landing transition
64 }
65
66 public void DoJump() => _anim.SetTrigger(Jump); // fire-and-forget; auto-resets after use
67}
68```
69
70### 2. Smooth a noisy input into a blend parameter
71
72```csharp
73// dampTime smooths Speed so the blend tree doesn't snap; great for analog sticks.
74_anim.SetFloat(Speed, targetSpeed, 0.1f /* dampTime */, Time.deltaTime);
75```
76
77### 3. Play / cross-fade a state directly (bypassing parameter conditions)
78
79```csharp
80// Useful for hit reactions where you want an immediate, explicit transition.
81_anim.CrossFade("Hit", 0.1f); // blend over 0.1s normalized
82// Or jump instantly: _anim.Play("Hit");
83```
84
85### 4. Wait until the current state finishes
86
87```csharp
88private System.Collections.IEnumerator AfterAttack()
89{
90 var info = _anim.GetCurrentAnimatorStateInfo(0); // layer 0
91 yield return new WaitForSeconds(info.length); // approximate clip length
92 // ...follow-up logic
93}
94```
95
96## Pitfalls
97
98- **`SetTrigger` missed or "sticks"** — triggers are consumed by the next satisfied transition
99 and auto-reset; if no transition consumes it, it can fire later unexpectedly. Use
100 `ResetTrigger` to clear, or prefer a `Bool` when the condition is a sustained state.
101- **String parameter typos fail silently** — a misspelled name just does nothing. Use
102 `Animator.StringToHash` and cache the int hashes.
103- **Transition feels laggy** — `Has Exit Time` makes the transition wait for the clip to reach
104 a normalized time. Uncheck it for responsive, condition-driven transitions (jump, hit).
105- **Character slides or won't move** — `Apply Root Motion` is on but your code also moves the
106 transform (or vice versa). Decide: root motion *or* scripted movement, not both.
107- **Upper-body layer overrides the whole body** — set the layer's Blend mode (Override vs
108 Additive), assign an Avatar Mask, and tune `layerWeight` (0–1).
109- **IK does nothing** — IK only applies inside `OnAnimatorIK`, requires "IK Pass" enabled on
110 the layer, and needs a Humanoid Avatar.
111
112## References
113
114- For **blend trees** (1D vs 2D Freeform/Directional), **animation layers + Avatar Masks**,
115 and **humanoid IK** (`OnAnimatorIK`, `SetIKPositionWeight`, `SetIKPosition`, look-at), read
116 `references/blend-trees-and-ik.md`.
117- Primary docs: Unity Manual "Animation" section and `ScriptReference/Animator`.
118
119## Related skills
120
121- `unity-csharp-scripting` — the MonoBehaviour and coroutine timing used above.
122- `unity-physics` — moving the body that the animation visualises.
123- `game-ai` — deciding *when* to play which animation state.