Course contentsShow
← Unity Game Development
Lesson 427 of 8286. Animation and CinemachineFree lesson

Smoothing Procedural Movement with Lerp

Students will implement linear interpolation to prevent jittery foot snapping and ensure fluid, natural transitions between terrain elevations.

In this lesson, you will learn how to use Linear Interpolation (Lerp) to eliminate "foot snapping" and create fluid, natural movement transitions when your character encounters uneven terrain.

The Problem: Instant Snapping

When using raycasting and IK to place a character's feet, the target position often updates instantly every frame. If your character walks over a rock or a step, the foot position snaps vertically the millisecond the ray hits the new elevation. This results in robotic, jittery movement that ruins the sense of weight and realism in your cinematic sequences.

The Solution: Smoothing with Lerp

Instead of snapping the foot to the raw hit position immediately, we treat the current foot position as an object moving toward a target. By using Mathf.Lerp (or Vector3.Lerp), we tell the foot to travel only a small fraction of the distance between its current position and its target position each frame.

Consider this implementation logic:

// CurrentFootPos is the position the foot is currently occupying
// TargetFootPos is the point hit by our raycast on the terrain
// SmoothingSpeed determines how fast the foot catches up to the target

Vector3 smoothedPosition = Vector3.Lerp(currentFootPos, targetFootPos, Time.deltaTime * smoothingSpeed);
currentFootPos = smoothedPosition;

Why this works

By multiplying the interpolation factor by Time.deltaTime, we make the transition frame-rate independent. As the character moves, the foot doesn't teleport; it glides smoothly toward the new elevation. If the smoothingSpeed is high, the movement feels snappy and responsive; if it is lower, the foot feels like it has more "weight," trailing slightly behind the ground changes to create a natural, organic flow.

This technique acts as a low-pass filter for your procedural animation data, turning erratic, frame-by-frame jumps into a graceful arc of movement. It ensures that even when your IK system detects complex surface changes, the visual output remains polished and professional.

Key Takeaway: Never move procedural targets instantly; use Lerp to bridge the gap between your calculated position and your current position, allowing the foot to transition fluidly over time rather than snapping frame-by-frame.