| Type | Class (Component) |
|---|---|
| Namespace | UnityEngine |
| Inherits | Behaviour > Component > Object |
| Needs | An 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.
| Type | Set with | Use for |
|---|---|---|
| Float | SetFloat | Speed, blend trees (walk → run) |
| Int | SetInteger | Weapon type, combo step |
| Bool | SetBool | States that last: IsGrounded, IsCrouching |
| Trigger | SetTrigger | One-off events: Jump, Attack, Hit. Resets itself after being used. |
Speed and fires Jump; the highlighted transitions check those parameters and the Animator changes state on its own.Properties
| Name | Type | Description |
|---|---|---|
speed | float | Playback speed multiplier. 1 = normal. |
runtimeAnimatorController | RuntimeAnimatorController | The controller in use. Can be swapped at runtime. |
applyRootMotion | bool | If true, the animation itself moves the character (e.g. walking clips that move forward). |
updateMode | AnimatorUpdateMode | Normal, Fixed (in sync with physics), or Unscaled Time (plays when paused). |
Methods
| Method | Returns | Description |
|---|---|---|
SetFloat(string name, float value) | void | Set a float parameter. |
SetBool(string name, bool value) | void | Set a bool parameter. |
SetInteger(string name, int value) | void | Set an int parameter. |
SetTrigger(string name) | void | Fire a trigger. |
ResetTrigger(string name) | void | Cancel a trigger that hasn't been used yet. |
GetFloat, GetBool, GetInteger | varies | Read parameters. |
Play(string stateName) | void | Jump straight to a state, skipping transitions. |
CrossFade(string stateName, float duration) | void | Blend smoothly into a state over duration (normalized time). |
GetCurrentAnimatorStateInfo(int layer) | AnimatorStateInfo | Info about the playing state: IsName("Attack"), normalizedTime (0 = start, 1 = end of first loop). |
Animator.StringToHash(string name) | int | Static. 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
- Your
Updatesets parameters. Nothing visible happens yet. - After all
Updatecalls, the Animator evaluates the current state's transitions. If a condition is met, it starts blending to the next state. - It samples the animation clips and writes the resulting positions/rotations into the bone Transforms.
LateUpdateruns after this, so it's where you'd override a bone (e.g. make the head look at something).- 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
ResetTriggerif needed.
Common Mistakes
- "Parameter 'speed' does not exist". Names are case-sensitive and must match the Parameters tab exactly.
- Animations feel delayed. Select the transition and untick Has Exit Time (otherwise it waits for the clip to finish) and lower Transition Duration.
- Calling
Playevery frame. Restarts the animation constantly. Use parameters and transitions instead. - The character slides or doesn't move. Check
applyRootMotion: on means the animation moves the object, off means your script does.
See also
- Tutorial: Animating with DOTween (for simple code-driven motion: UI, pickups, doors)
This page is a simplified, AI-assisted interpretation. For the complete and authoritative reference, see the official Unity documentation.