Field Notes: Animation
Animation bugs are the ones players notice first: feet that skate across the floor, a character that pops back to the start of a loop, an arm that spins the long way round during a blend, a footstep sound that plays twice or never. Each has a precise cause. These notes collect the common ones, with small models of why they happen.
1. Foot sliding: match the clip speed to the capsule speed
A locomotion clip is authored at a speed: the walk cycle covers 1.4 m in one loop, so it looks right at 1.4 m/s only. When gameplay moves the capsule at a different speed and the clip plays at normal rate, the planted foot has to slide by the difference. Blend trees fix most of it by blending walk and run by speed, but only if the blended clip is also played at a rate that matches the actual speed (often called stride or speed warping):
#include <iostream>
#include <iomanip>
#include <algorithm>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double walkSpeed = 1.4, runSpeed = 3.5; // authored root speeds of the two clips (m/s)
std::cout << "speed run weight clip speed slide (rate 1) playback rate\n";
for (double speed : {0.7, 1.4, 2.0, 3.5, 4.5}) {
double w = std::clamp((speed - walkSpeed) / (runSpeed - walkSpeed), 0.0, 1.0); // 1D blend tree
double clipSpeed = walkSpeed + (runSpeed - walkSpeed) * w;
double slide = speed - clipSpeed; // m/s the planted foot skates
double rate = speed / clipSpeed; // play faster or slower to match
std::cout << std::setw(5) << speed << " " << std::setw(10) << w << " " << std::setw(10) << clipSpeed
<< " " << std::setw(14) << slide << " " << std::setw(13) << rate << "\n";
}
}speed run weight clip speed slide (rate 1) playback rate
0.700 0.000 1.400 -0.700 0.500
1.400 0.000 1.400 0.000 1.000
2.000 0.286 2.000 0.000 1.000
3.500 1.000 3.500 0.000 1.000
4.500 1.000 3.500 1.000 1.286
Between 1.4 and 3.5 m/s the blend alone keeps feet planted, because the blended clip speed follows the capsule (assuming the walk and run clips are synchronized by phase, which blend trees do for looping clips). Outside that range, at a slow 0.7 m/s or a sprint at 4.5 m/s, the feet skate unless you change the playback rate; past about ±30% the cadence looks wrong and you need another clip (a slow walk, a sprint). Measure the authored speed of every locomotion clip once (root displacement ÷ duration) and store it with the clip.
2. Root motion: decide who moves the capsule
With root motion, the clip's root bone displacement is taken out of the pose and applied to the character (Blend Trees, Root Motion and Events). Mixing the two models is the classic bug: the clip has root motion, but the code also moves the capsule, or root motion is extracted but never applied. Then the mesh walks away from its collider for one loop and snaps back when the clip wraps around. Pick one owner per state:
- Code-driven (most gameplay locomotion, all networked characters): in-place clips or root motion disabled, capsule moved by the controller, playback rate matched as above.
- Animation-driven (attacks, vaults, cinematic moves, climbing): root motion enabled, and the delta fed into the character controller (Unity
OnAnimatorMove→CharacterController.Move; Unreal root motion montages consumed by the CMC; Godotget_root_motion_position()→velocity→move_and_slide()), so collisions still apply.
Never write root motion straight into the transform of something with a collider; it walks through walls.
3. Blend quaternions in the same hemisphere
q and -q are the same rotation. Animation data, IK solvers and conversions from Euler angles can produce either one. If you blend q1 with a q2 that sits in the opposite hemisphere (dot(q1, q2) < 0), lerp and nlerp go the long way, through more than 180°, and halfway through the blend the bone points somewhere neither pose intended. The fix is one line: negate q2 when the dot product is negative (Slerp).
#include <iostream>
#include <iomanip>
#include <cmath>
struct Q { double w, x, y, z; };
const double PI = 3.141592653589793;
Q yaw(double deg) { double h = deg * PI / 360; return {std::cos(h), 0, std::sin(h), 0}; }
double dot(Q a, Q b) { return a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z; }
Q nlerp(Q a, Q b, double t, bool fixHemisphere) {
if (fixHemisphere && dot(a, b) < 0) b = {-b.w, -b.x, -b.y, -b.z};
Q r{a.w + (b.w - a.w) * t, a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t};
double n = std::sqrt(dot(r, r));
return {r.w / n, r.x / n, r.y / n, r.z / n};
}
double yawDeg(Q q) { return 2 * std::atan2(q.y, q.w) * 180 / PI; }
int main() {
std::cout << std::fixed << std::setprecision(3);
Q a = yaw(170);
Q b = yaw(-170); // 20 degrees away from a, but stored in the other hemisphere
std::cout << "dot(a, b) = " << dot(a, b) << "\n";
for (double t : {0.25, 0.5, 0.75})
std::cout << "t " << t << ": naive " << std::setw(8) << yawDeg(nlerp(a, b, t, false))
<< " deg, fixed " << std::setw(8) << yawDeg(nlerp(a, b, t, true)) << " deg\n";
}dot(a, b) = -0.985
t 0.250: naive 160.150 deg, fixed 174.990 deg
t 0.500: naive 0.000 deg, fixed 180.000 deg
t 0.750: naive -160.150 deg, fixed 185.010 deg
The fixed blend takes the short 20° arc through 180° (185° is the same heading as -175°), while the naive one goes the long way: at the midpoint it faces 0°, the opposite direction of both poses, so the character spins almost all the way round for a 20° blend. Engines do this check for you in their own blend code; your own IK, procedural aim or network-compressed rotations have to do it themselves.
4. Animation events: fire on crossing, not on equality
A footstep event at 0.5 s will never see time == 0.5 at 30 FPS. Fire every event whose time lies in (previousTime, currentTime], and handle the wrap-around when a looping clip passes its end, or events are skipped at low frame rates and doubled when a frame lands exactly on them twice:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double clipLength = 1.0, dt = 1.0 / 30;
const std::vector<double> events{0.0, 0.5}; // left and right footsteps
double t = 0.91; // start near the end of the loop
int naive = 0, crossing = 0;
for (int frame = 0; frame < 30; frame++) {
double prev = t;
t += dt;
bool wrapped = t >= clipLength;
if (wrapped) t -= clipLength;
for (double e : events) {
if (std::fabs(t - e) < 0.001) naive++; // equality-style check
bool inRange = wrapped ? (e > prev || e <= t) : (e > prev && e <= t);
if (inRange) crossing++;
}
}
std::cout << "one second of walking = 2 footsteps\n";
std::cout << "equality check fired " << naive << ", crossing check fired " << crossing << "\n";
}one second of walking = 2 footsteps
equality check fired 0, crossing check fired 2
The equality check only fires when a frame happens to land exactly on an event time, which at 30 FPS depends on rounding luck. Engines implement the crossing rule, but they can still skip events in two situations you control: when a clip is blended out below the event weight threshold, and when the character is culled (off-screen animators may stop updating). Gameplay-critical events, such as the frame where an attack starts dealing damage, belong in gameplay code or in events that are configured to fire regardless of weight.
5. Retargeting: scale root motion with the character
An animation authored on a 1.8 m character moves the root and hips in that character's units. Played on a 1.2 m character without adjustment, every stride is too long for its legs (feet slide) and root motion carries it too far. Retargeters scale translations by a ratio, usually hip height:
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double sourceHip = 1.00, strideSource = 0.75; // authored on a character with 1.0 m hip height
for (double targetHip : {1.00, 0.67, 1.25}) {
double scale = targetHip / sourceHip;
std::cout << "hip " << targetHip << ": stride unscaled " << strideSource << " m ("
<< strideSource / targetHip << " leg lengths), scaled " << strideSource * scale << " m ("
<< strideSource * scale / targetHip << " leg lengths)\n";
}
}hip 1.000: stride unscaled 0.750 m (0.750 leg lengths), scaled 0.750 m (0.750 leg lengths)
hip 0.670: stride unscaled 0.750 m (1.119 leg lengths), scaled 0.503 m (0.750 leg lengths)
hip 1.250: stride unscaled 0.750 m (0.600 leg lengths), scaled 0.938 m (0.750 leg lengths)
Scaled, every character takes strides of the same 0.75 leg lengths, which is what makes the motion look natural. Unity's Humanoid retargeting and Unreal's IK Retargeter do this from hip height; when you write your own, scale root and pelvis translations and leave rotations alone.
6. Short notes
- Update physics-driven animation in the fixed step. If animation moves rigid bodies or kinematic colliders, step it with physics (Unity
AnimatorUpdateMode.Fixed, Godot's AnimationMixer physics callback mode), or contacts lag one frame behind the visuals. - Run IK after animation, before rendering, and fade IK weights in and out; turning foot IK on in a single frame snaps the leg.
- Short transitions. 0.1–0.25 s cross-fades cover most locomotion; long blends between very different poses look like the character is underwater.
- Off-screen characters. Culling saves CPU, but culled animators stop root motion and events too. Keep gameplay-relevant characters animating, or drive them from code while culled.
- Four bone influences per vertex is the common budget for linear blend skinning in real-time engines; renormalize weights after editing (Skinning).
- Compression errors show at the extremities. A tiny error at the hip becomes a visible error at the fingertips and feet; tune compression error thresholds by measuring at end bones.
- Keep a T-pose or A-pose reference clip for every skeleton, and check retargeting on it first.
Test yourself
A walk clip authored at 1.4 m/s plays at normal speed while the capsule moves at 2.1 m/s with no blend. What does the player see?
- Nothing wrong
- Feet sliding forward at 0.7 m/s; fix by blending to a faster clip or playing at 1.5x
- The character floats
- A T-pose
nlerp between two quaternions 20° apart swings the bone almost all the way round. What is missing?
- Normalization
- Negating one quaternion when their dot product is negative
- Converting to Euler angles
- More keyframes
How should a footstep event at 0.5 s be detected each frame?
time == 0.5- Fire if 0.5 lies in (previousTime, currentTime], handling the loop wrap-around
- Every frame after 0.5
- Only on frames that are multiples of 0.5