Animation and timelines
Drive an Animator with states and parameters, and sequence cutscenes with Timeline.
- Build states and transitions in an Animator Controller.
- Use a trigger for one-shot actions and a float or bool for ongoing states.
- Drive parameters from a script.
- Sequence cutscenes with Timeline tracks.
In plain English
An Animator is the component that plays animations through a controller made of states and transitions. A parameter is a variable on the Animator, such as a bool, float or trigger, that a script can change to tell the controller what to do. A trigger is a one-shot parameter you set to fire a transition like a jump, while a bool or float describes an ongoing state such as running. Timeline is a sequencer that arranges animation clips, audio and activation tracks into a cutscene on a single time ruler.
Step by step
- 1Add an Animator component and assign an Animator Controller.
- 2Create a state and a transition in the Animator window.
- 3Add a trigger parameter and a transition to a jump state.
- 4Set the trigger from a script when the player jumps.
using UnityEngine;
public class PlayerAnimator : MonoBehaviour
{
[SerializeField] private Animator animator;
private void Update()
{
if (Input.GetButtonDown("Jump"))
{
animator.SetTrigger("Jump");
}
}
}Common mistakes
- Naming the parameter in code with a different case than in the controller fails silently, so the animation never changes.
- Using a trigger for an ongoing state such as running fires once and never re-evaluates; use a bool or float instead.
- Hardcoding cutscene logic onto gameplay objects couples a cinematic to one scene and makes it hard to reuse.
- Forgetting to assign the Animator reference in the Inspector leaves it null and causes a runtime error.
Trigger a jump
Write an Update method that sets the Jump trigger on the Animator when the jump button is pressed.
using UnityEngine;
public class PlayerAnimator : MonoBehaviour
{
[SerializeField] private Animator animator;
private void Update()
{
}
}Which parameter type is best for a one-shot action such as a jump?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.