Building Two-Dimensional Directional Blend Spaces
In this lesson, you will learn how to transition between animations seamlessly across two axes—horizontal and vertical—to create fluid, multidimensional character movement.
Moving Beyond One Dimension
Previously, we used a one-dimensional Blend Tree to transition between states like "Idle" and "Run" based on a single speed value. However, a 2D Blend Tree allows you to map animations to a coordinate system (X and Y), such as mapping Velocity X and Velocity Y to a set of walking or running animations in every direction (North, South, East, West).
Setting Up the 2D Blend Tree
To create this, open your Animator Controller, right-click in the grid, and select Create State > From New Blend Tree. Double-click the tree to enter its graph. In the Inspector, change the Blend Type to 2D Freeform Directional.
You will need two parameters, usually named MoveX and MoveY. These represent the direction your character is currently facing or moving. Once set, you add your animation clips (like WalkForward, WalkBack, WalkLeft, WalkRight) to the list. Each clip is assigned a Pos X and Pos Y value:
- Forward: (0, 1)
- Backward: (0, -1)
- Right: (1, 0)
- Left: (-1, 0)
- Idle: (0, 0)
Unity uses these coordinates to calculate a weighted average. If your script sets MoveX to 0.5 and MoveY to 0.5, Unity will interpolate between your "Forward" and "Right" animations, blending the frames perfectly so the character appears to walk diagonally.
Bridging the Gap
To drive this visually, your script must pass movement input to the Animator:
void Update() {
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
animator.SetFloat("MoveX", x);
animator.SetFloat("MoveY", y);
}
By keeping these parameters updated every frame, the Blend Tree automatically calculates the correct pose based on the character's movement vector, creating a smooth, professional feel without requiring dozens of hard-coded state transitions.
Key Takeaway: 2D Blend Trees use X and Y coordinate parameters to interpolate between multiple animation clips, allowing for fluid, directional movement that reacts naturally to player input.