In this lesson, you will learn how to map your character’s input axes to Blend Trees to achieve fluid, realistic movement transitions instead of relying on jarring, abrupt animation swaps.
Beyond the State Machine
While Animator State Machines are excellent for handling discrete events like "Jumping" or "Attacking," they struggle with continuous movement. If you transition directly from "Idle" to "Walk," the jump between the two clips can look robotic. A Blend Tree solves this by mathematically interpolating between multiple animation clips based on parameter values, creating a seamless "in-between" frame state.
Mapping Inputs to Motion
To implement this, you define a float parameter (like Speed) in your Animator. In a 1D Blend Tree, you add your animation clips—Idle, Walk, and Run—and assign them a threshold value. As your script updates the Speed parameter, Unity blends the animations based on how close the value is to each threshold.
For example, if your Speed is 0.5, Unity will blend the last 50% of your Idle animation with the first 50% of your Walk animation.
// Update the Animator parameter based on character velocity
void Update() {
float moveSpeed = characterController.velocity.magnitude;
animator.SetFloat("Speed", moveSpeed);
}
1D vs. 2D Blending
- 1D Blend Trees: Best for linear movement, like increasing speed from a standstill to a sprint. It uses a single parameter to traverse a list of animations.
- 2D Blend Trees: Essential for directional movement. By mapping two parameters (like
HorizontalandVertical) to a 2D plane, you can blend animations based on the character’s facing direction and intensity. This allows a character to transition smoothly from walking forward to strafing left, or turning while moving, simply by feeding the input axes directly into the Animator.
By treating animation as a continuous spectrum of data rather than a series of rigid clips, your character’s locomotion will feel reactive and professional.
Key Takeaway: Blend Trees eliminate robotic transitions by mathematically interpolating between animation clips based on real-time input parameters, ensuring your character’s movement feels fluid and responsive.