Blend Trees, Root Motion and Events
Blending combines poses. This lesson covers the three things that connect blended animation to gameplay: blend trees that choose the weights from game parameters, root motion that lets the animation move the character, and animation events that let it trigger sounds and gameplay at the right frame.
1D blend trees
A walk clip is authored for one speed, a run clip for another. To move at any speed in between without sliding feet, place the clips on a parameter axis (speed) and blend the two neighbours of the current value:
- at 0.75 m/s: half idle, half walk;
- at 2.75 m/s: half walk, half run.
Blending two cycles of different lengths adds a problem: a 1.2 s walk and a 0.8 s run played side by side drift out of phase, and the blend shows a left foot from one clip and a right foot from the other. The fix is sync by normalized time: both clips advance by the same fraction of their cycle each frame, using a blended cycle length. Each clip then plays at its own rate (clipLength / cycleLength), and their feet land together.
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
// A 1D blend tree: clips placed on a "speed" axis at the speed they were authored for.
struct Clip { std::string name; double speed, length; }; // speed in m/s, cycle length in s
const std::vector<Clip> clips = {{"idle", 0.0, 2.0}, {"walk", 1.5, 1.2}, {"run", 4.0, 0.8}};
struct Blend { int a, b; double w; }; // w = weight of clip b
Blend evaluate(double speed) {
speed = std::clamp(speed, clips.front().speed, clips.back().speed);
for (int i = 0; i + 1 < int(clips.size()); i++)
if (speed <= clips[i + 1].speed) {
double w = (speed - clips[i].speed) / (clips[i + 1].speed - clips[i].speed);
return {i, i + 1, w};
}
return {0, 0, 0};
}
int main() {
std::cout << std::fixed << std::setprecision(3);
std::cout << "speed blend cycle length playback rate walk/run\n";
for (double speed : {0.0, 0.75, 1.5, 2.0, 2.75, 4.0, 5.0}) {
Blend bl = evaluate(speed);
const Clip& A = clips[bl.a]; const Clip& B = clips[bl.b];
// Sync: both clips advance by the same normalized phase, using a blended cycle length.
double cycle = A.length + (B.length - A.length) * bl.w;
std::cout << std::setw(5) << speed << " " << std::setw(4) << A.name << " " << 1 - bl.w << " + "
<< std::setw(4) << B.name << " " << bl.w << " " << std::setw(8) << cycle << "s";
if (A.name != "idle")
std::cout << " " << A.length / cycle << " / " << B.length / cycle;
std::cout << "\n";
}
}speed blend cycle length playback rate walk/run
0.000 idle 1.000 + walk 0.000 2.000s
0.750 idle 0.500 + walk 0.500 1.600s
1.500 idle 0.000 + walk 1.000 1.200s
2.000 walk 0.800 + run 0.200 1.120s 1.071 / 0.714
2.750 walk 0.500 + run 0.500 1.000s 1.200 / 0.800
4.000 walk 0.000 + run 1.000 0.800s 1.500 / 1.000
5.000 walk 0.000 + run 1.000 0.800s 1.500 / 1.000
At 2.75 m/s the blended cycle is 1.0 s, so the walk plays 1.2× faster and the run 0.8× slower, and both put down a foot at the same moment. Above the last clip (5 m/s) the tree clamps to pure run; games usually scale the playback rate up there instead (5 / 4 = 1.25×), within limits that still look right.
Sync requires clips authored with matching phases (left foot down at 0% in every cycle). When clips don't match, engines offer sync markers (Unreal's Sync Markers, for example) placed on the foot contacts.
2D blend spaces
Strafing characters need two parameters: forward speed and sideways speed. A 2D blend space places clips on a plane (forward walk, back walk, strafe left/right, diagonals, runs) and blends the three or four nearest ones, by triangulation or by gradient-band weighting. Unity calls these 2D Blend Trees (Simple/Freeform Directional, Freeform Cartesian), Unreal Blend Spaces, Godot AnimationNodeBlendSpace2D. All weights are normalized to sum to 1.
Root motion
There are two ways for an animated character to move through the world:
- In-place animation + code movement: the clip walks on the spot, and the controller moves the capsule at a speed set by code. Responsive and predictable, but the feet slide if the speed doesn't match the clip.
- Root motion: the clip moves its root bone forward as the character really walked, and the engine extracts that motion each frame, removes it from the pose and applies it to the character's controller. No foot sliding, and motion with complex timing (a lunge, a vault, a stumble) matches the animation exactly.
Extraction is a difference: the root's position at the current sample minus its position at the previous sample. When a looping clip wraps, the delta is "the rest of the old cycle" plus "the start of the new one". Events are found the same way, as everything that lies in the half-open window (previous, current]:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
struct Key { double t, z; };
// The root bone's forward position in a 1.0 s walk cycle: 1.4 m per cycle, faster mid-stride.
const std::vector<Key> rootTrack = {{0.0, 0.0}, {0.25, 0.30}, {0.5, 0.70}, {0.75, 1.00}, {1.0, 1.40}};
const double LENGTH = 1.0;
struct Event { double t; std::string name; };
const std::vector<Event> events = {{0.10, "footstep_left"}, {0.60, "footstep_right"}};
double rootAt(double t) {
for (size_t i = 0; i + 1 < rootTrack.size(); i++)
if (t <= rootTrack[i + 1].t) {
double u = (t - rootTrack[i].t) / (rootTrack[i + 1].t - rootTrack[i].t);
return rootTrack[i].z + (rootTrack[i + 1].z - rootTrack[i].z) * u;
}
return rootTrack.back().z;
}
// Fire every event in the half-open window (from, to], handling the wrap at the loop point.
void fireEvents(double from, double to, bool wrapped) {
for (const Event& e : events) {
bool hit = wrapped ? (e.t > from || e.t <= to) : (e.t > from && e.t <= to);
if (hit) std::cout << " event " << e.name << " (t=" << e.t << ")\n";
}
}
int main() {
std::cout << std::fixed << std::setprecision(3);
double clipTime = 0.0, characterZ = 0.0;
const double dt = 0.3;
for (int frame = 1; frame <= 6; frame++) {
double prev = clipTime;
clipTime = std::fmod(clipTime + dt, LENGTH);
bool wrapped = clipTime < prev;
// Root motion = how far the root moved during this frame. Across the loop point, add
// the rest of the old cycle and the start of the new one.
double delta = wrapped ? (rootAt(LENGTH) - rootAt(prev)) + (rootAt(clipTime) - rootAt(0))
: rootAt(clipTime) - rootAt(prev);
characterZ += delta;
std::cout << "frame " << frame << ": clip " << prev << " -> " << clipTime << (wrapped ? " (wrapped)" : " ")
<< " root delta " << delta << " character z " << characterZ << "\n";
fireEvents(prev, clipTime, wrapped);
}
}frame 1: clip 0.000 -> 0.300 root delta 0.380 character z 0.380
event footstep_left (t=0.100)
frame 2: clip 0.300 -> 0.600 root delta 0.440 character z 0.820
event footstep_right (t=0.600)
frame 3: clip 0.600 -> 0.900 root delta 0.420 character z 1.240
frame 4: clip 0.900 -> 0.200 (wrapped) root delta 0.400 character z 1.640
event footstep_left (t=0.100)
frame 5: clip 0.200 -> 0.500 root delta 0.460 character z 2.100
frame 6: clip 0.500 -> 0.800 root delta 0.380 character z 2.480
event footstep_right (t=0.600)
The character moves forward by the root deltas: 0.38 m, 0.44 m, 0.42 m, then 0.40 m across the loop point (0.16 m left in the old cycle plus 0.24 m into the new one). Ignoring the wrap would give rootAt(0.2) - rootAt(0.9) = -1.0: the character would jump back a metre every cycle, a bug that shows up in almost every hand-written root motion system once.
The event window matters too: (prev, current] fires each event exactly once, including the left footstep at 0.1 s that is passed while wrapping in frame 4. Testing t == eventTime would miss events between frames, and a closed window [prev, current] would fire events on frame boundaries twice.
Note: Root motion moves the character, not just the mesh, so the extracted delta still goes through the character controller (Collide and Slide): walls stop it, and gravity is often still added by code. Networked games often keep root motion for a few special moves (attacks, vaults) and in-place animation for locomotion, because code-driven movement is easier to predict and replicate.
Engine mapping
// Unity 6 (not runnable here)
static readonly int SpeedId = Animator.StringToHash("Speed");
void Update() => animator.SetFloat(SpeedId, controllerVelocity.magnitude, 0.1f, Time.deltaTime); // damped
// Root motion: tick "Apply Root Motion" (animator.applyRootMotion), or take over in
void OnAnimatorMove() { characterController.Move(animator.deltaPosition); transform.rotation *= animator.deltaRotation; }
// Events: Animation Events added on the clip call a method of a script on the same GameObject.# Godot 4 (not runnable here)
$AnimationTree.set("parameters/locomotion/blend_position", velocity.length())
var playback = $AnimationTree.get("parameters/playback") # AnimationNodeStateMachinePlayback
playback.travel("Jump")
# Root motion: set AnimationTree.root_motion_track, then read get_root_motion_position() each frame.
# Events: Call Method tracks in the Animation call functions at keyed times.In Unreal, the Animation Blueprint's AnimGraph holds Blend Spaces and state machines; root motion is enabled per sequence and montage (with the Root Motion Mode on the Anim Instance), and events are Anim Notifies and Notify States placed on the timeline.
Test yourself
Why do blended walk and run cycles need to be synced by normalized time?
- To save memory
- Cycles of different lengths drift out of phase, so the blend mixes a left-foot pose with a right-foot pose
- Because blend weights must sum to 1
- To enable root motion
A root-motion clip loops from 0.9 s back to 0.2 s in one frame. How is the root delta computed?
root(0.2) - root(0.9)(root(end) - root(0.9)) + (root(0.2) - root(0))- Zero, because the clip wrapped
root(0.9) - root(0.2)
Which window fires each animation event exactly once?
t == eventTime[previous, current](previous, current], with wrap handling(previous, current)