Skip to content
Back to ideas & experiments
01 Jul 2022
2 min read
Game Development

Unity – using a Blend Tree for an eight-direction 2D character

Setting up eight-direction walk animations for a Ragnarok Online-style 2D character with a Unity 2D Freeform Cartesian blend tree.
閱讀中文版 →
Unity – Blend Tree應用

A 2D character in the style of Ragnarok Online needs eight facing directions. Rather than picking the right animation with a pile of if-statements, let a Unity blend tree do it: pass the character’s movement direction on the x/z plane into the Animator, and it selects the matching animation.

Eight head sprites of a green-haired chibi character, one for each facing direction
One character, eight directions.

1. Prepare eight directional animations

Make one walk animation (a sprite sequence) per direction and name them by compass point: moveN, moveNE, moveE, moveSE, moveS, moveSW, moveW, moveNW. Consistent names make the Animator much easier to set up and debug.

2. Build the blend tree

  1. Create a Blend Tree in the Animator and set its Blend Type to 2D Freeform Cartesian.
  2. Add two float parameters, MoveX and MoveY, and assign them to the tree.
  3. Add the eight clips and position them by direction:
MotionMoveXMoveY
moveN01
moveNE0.750.75
moveE10
moveSE0.75−0.75
moveS0−1
moveSW−0.75−0.75
moveW−10
moveNW−0.750.75

The diagonals use 0.75 rather than 1 so that all eight points sit roughly on the same circle, giving each direction a similar share of the space.

Unity editor with a 2D Freeform Cartesian blend tree: eight move clips placed around the MoveX / MoveY graph
The actual setup: eight clips arranged around the MoveX / MoveY graph.

3. Drive it from code

The character moves across the ground of a 3D scene, so the x and z components of its velocity feed MoveX and MoveY:

// Feed the blend tree from the character's movement on the ground plane (x, z).
Vector3 v = rb.linearVelocity;
Vector2 move = new Vector2(v.x, v.z);
float speed = move.magnitude;
animator.SetFloat("Speed", speed);

// Only update the facing while moving, so an idle character keeps looking the way it last walked.
if (speed > 0.05f)
{
    move /= speed;
    animator.SetFloat("MoveX", move.x);
    animator.SetFloat("MoveY", move.y);
}

Updating the direction only while moving matters: otherwise the parameters drop to zero the moment the character stops, and it snaps back to a default facing.

Why can sprite animations “blend” at all?

Skeletal 3D animations can be interpolated, but sprites are discrete images. For properties that cannot be interpolated, the Animator uses the clip with the highest weight, so the blend tree effectively picks the nearest direction, which is exactly what an eight-direction character needs.

Unity also has a 2D Simple Directional type, designed for one clip per direction, which suits this case just as well. If you later add a run animation in the same directions, 2D Freeform Directional is the better fit.

Reference

freeCodeCamp’s Unity 3D Tutorial – Beat Em Up Fight Game walks through a related Animator setup.