In this lesson, you will learn how to use Animator Parameters as the "control panel" that tells your State Machine exactly when to switch between animation clips.
The Role of Animator Parameters
Think of an AnimatorController as a set of rules, and Parameters as the data points that trigger those rules. Inside the Animator window, the Parameters tab allows you to define variables that your script can communicate with to dictate behavior. There are four types, each serving a specific logic:
- Bool: Perfect for binary states (e.g.,
isGrounded). If it’s true, perform the animation; if false, stop. - Float: Used for ranges (e.g.,
speed). This is great for blending, like transitioning from a walk to a run based on movement velocity. - Trigger: A one-time event (e.g.,
jump). It resets automatically after the transition fires, making it ideal for momentary actions. - Int: Used for discrete states, like selecting a specific attack animation from a numbered index.
Setting Up the Logic
To trigger a transition, you must link these parameters to the arrows (transitions) connecting your animation states. Select a transition arrow in the Animator window and, in the Conditions section of the Inspector, add your parameter.
For example, if you have a jump trigger, set the condition to If Jump is fired. When you call this from your C# script, the Animator immediately shifts to that state:
public Animator anim;
void Update() {
if (Input.GetButtonDown("Jump")) {
// Tells the Animator to fire the 'jump' transition
anim.SetTrigger("jump");
}
float speed = rb.velocity.magnitude;
// Updates the float to determine walk vs run states
anim.SetFloat("speed", speed);
}
By referencing these exact names defined in the Animator window, your code creates a bridge between player input and the visual movement of your character. Always ensure your parameter names match exactly, as the system is case-sensitive and relies on string-based lookups to trigger the state machine logic.
Key Takeaway: Animator Parameters act as the bridge between your C# game logic and the Animator’s state machine, allowing you to drive complex character behavior through clean, conditional transitions.