Implementing Smooth Object Movement with Lerp
In this lesson, you will learn how to create fluid, professional-looking movement between two locations using linear interpolation, commonly known as Lerp.
The Concept of Lerp
When you simply set a transform.position to a new coordinate, the object "teleports" instantly. To move smoothly, we use Vector3.Lerp(start, end, t).
Think of Lerp as a percentage slider:
- If
tis0, you are at the start position. - If
tis1, you are at the end position. - If
tis0.5, you are exactly halfway between them.
By gradually increasing t over time, the object glides smoothly from point A to point B rather than jumping instantly.
Implementation Example
To move an object smoothly, we calculate the position inside our Update loop. Since Update runs every frame—and frame rates vary per computer—we must use Time.deltaTime to ensure the movement speed is consistent regardless of how fast the game is running.
public Transform startPoint;
public Transform endPoint;
public float speed = 1.0f;
private float journeyProgress = 0f;
void Update()
{
// Increase progress based on time
journeyProgress += Time.deltaTime * speed;
// Use Lerp to update the position
transform.position = Vector3.Lerp(startPoint.position, endPoint.position, journeyProgress);
}
Why it Matters
If you multiplied your speed by a static number like 0.1 per frame, a player with a high frame rate would see the object move faster than a player with a lower frame rate. By using Time.deltaTime, we tie the movement to real-world seconds. The Lerp function handles the math for us, interpolating the gap between our points so the object’s journey feels polished and predictable.
Key Takeaway: Use Vector3.Lerp with Time.deltaTime to achieve frame-rate independent, fluid movement between two points in 3D space.