Course contentsShow
← Unity Game Development
Lesson 370 of 8286. Animation and CinemachineFree lesson

Implementing Parameter-Driven State Transitions

You will utilize Boolean, Integer, and Trigger parameters to control when the Animator Controller switches between character states.

In this lesson, you will learn how to communicate between your C# scripts and the Animator Controller by using parameters to trigger dynamic state changes.

Understanding Parameter-Driven Transitions

In an Animator Controller, states are the individual animations (like "Idle" or "Run"). A transition is the arrow connecting them. Instead of letting transitions happen automatically, we use Parameters—variables defined inside the Animator window—as the "deciders." By setting these via script, you bridge the gap between player input and visual feedback.

The Three Parameter Types

Unity gives us three main tools to control these transitions:

  • Boolean (bool): Best for states that are "on" or "off," such as isGrounded. If isGrounded is true, we stay in the Walk state; if false, we transition to Jump.
  • Integer (int): Used for numbered categories, like movementMode. (0 for Idle, 1 for Walk, 2 for Run).
  • Trigger (trigger): A special one-time event that resets automatically, perfect for one-shot actions like Attack or Jump.

Connecting Script to Animator

To influence these parameters, you must reference the Animator component in your script. Here is how you might trigger an attack animation when a player presses the spacebar:

private Animator anim;

void Start() {
    anim = GetComponent<Animator>();
}

void Update() {
    if (Input.GetButtonDown("Fire1")) {
        // Triggers the transition immediately
        anim.SetTrigger("Attack");
    }
    
    // Updates a boolean based on horizontal movement
    float move = Input.GetAxis("Horizontal");
    anim.SetBool("isMoving", move != 0);
}

Setting Up the Transitions

Inside the Animator window, click on your transition arrow. In the Inspector, look at the Conditions list. Click the + button to add your parameter (like Attack). The transition will now only execute when your script sets that specific parameter to match the condition you defined. This creates a responsive system where the character's animation is always perfectly in sync with your game logic.

Key Takeaway: Parameters act as the bridge between your game logic and your animations, allowing your code to dictate state changes by toggling Booleans, Integers, or Triggers.