Unity  

Difference Between Vector3.MoveTowards and Vector3.Lerp in Unity

Unity

In Unity, handling smooth movement and interpolation between points or positions in 3D space is very common, especially in game development. Two of the most frequently used methods for moving objects are Vector3.MoveTowards and Vector3.Lerp. Both are part of Unity’s Vector3 struct and help transition a vector from one point to another, but they do so in fundamentally different ways.

Understanding the difference between these two is crucial for implementing movement and animation logic that fits your game’s needs.

Overview of Vector3.MoveTowards

What is Vector3.MoveTowards?

Vector3.MoveTowards moves a point directly towards a target point by a fixed maximum distance each call. It ensures the position moves at a consistent speed until it reaches the target. It never overshoots or goes beyond the target.

Method signature

Vector3.MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
  • current: The current position vector.
  • target: The target position vector.
  • maxDistanceDelta: The maximum distance to move towards the target in one step.

How does it work?

  • Each call to MoveTowards moves the current vector closer to the target by at most maxDistanceDelta.
  • If the target is closer than maxDistanceDelta, the returned vector will be exactly the target position.
  • This creates a constant-speed movement that is independent of the distance between points.

Use case

  • Moving an object smoothly toward a destination at a fixed speed.
  • Perfect when you want to move an object towards a target, frame by frame, without worrying about interpolation percentage.

Example

transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);

Here, the object moves toward targetPosition at a speed, updating each frame.

Overview of Vector3.Lerp

What is Vector3.Lerp?

Vector3.Lerp performs linear interpolation between two vectors based on a normalized parameter t that goes from 0 to 1. This means it returns a point somewhere between the two vectors, depending on t.

Method signature

Vector3.Lerp(Vector3 a, Vector3 b, float t)
  • a: Start position.
  • b: End position.
  • t: Interpolation factor, usually between 0 and 1.

How does it work?

  • When t = 0, it returns vector a.
  • When t = 1, it returns vector b.
  • When t is between 0 and 1, it returns a point linearly interpolated between a and b.
  • If t is outside [0,1], the function extrapolates beyond the start or end points.

Use case

  • When you want to smoothly interpolate between two fixed points based on a fraction.
  • Useful for animations or smooth transitions controlled by a parameter that progresses over time.
  • Often used when you want to control the interpolation progress yourself (e.g., by a timer or curve).

Example

float t = elapsedTime / duration; // elapsedTime goes from 0 to duration
transform.position = Vector3.Lerp(startPosition, endPosition, t);

Here, the object's position moves gradually from startPosition to endPosition over a specified duration.

Key Differences Between MoveTowards and Lerp

Aspect Vector3.MoveTowards Vector3.Lerp
Movement type Moves at a fixed maximum distance per call (constant speed) Moves based on interpolation factor t between 0 and 1 (percentage)
Parameters Current position, target position, max step distance Start position, end position, interpolation factor (0 to 1)
Behavior Never overshoots the target; stops exactly at the target Can overshoot if t is outside 0-1; interpolates or extrapolates
Speed control Speed controlled by max distance per frame/time Speed is controlled externally by how t changes over time
Use case Moving objects toward a target at a fixed speed Smooth transitions or animations with controlled interpolation progress
Typical usage Move objects with physics-like constant velocity Animate or blend between states or positions

Common Pitfalls

Using Lerp Without Clamping t

If you pass a t value greater than 1 or less than 0, Lerp will extrapolate beyond the endpoints, which can cause unexpected behavior. Always clamp t if you want strict interpolation within the start and end points.

t = Mathf.Clamp01(t);

Misusing MoveTowards for Smooth Interpolation

MoveTowards moves by fixed step sizes each frame, so the actual time to reach the target depends on the distance. It is not suitable when you want movement to be completed in a fixed duration, regardless of distance.

When to Use Which?

  • Use MoveTowards if you want an object to move towards a target at a constant speed, e.g., an enemy chasing the player or a projectile moving steadily.
  • Use Lerp if you want to interpolate between two positions over time, such as moving a UI element smoothly or animating a transition where the progress percentage t is controlled by a timer or curve.

Summary

Feature Vector3.MoveTowards Vector3.Lerp
Moves a vector toward the target by a fixed step size Yes No
Moves at constant speed irrespective of distance Yes No (depends on how t changes)
Requires normalized parameter (0 to 1) No Yes
Stops exactly at the target Yes Can overshoot if t out of range
Best for frame-based movement Yes Best for time-based interpolation

If you are still unsure which to use, here is a simple analogy.

  • Moving toward is like walking toward a door by taking equal steps each second until you reach the door.
  • Lerp is like standing between your home and the door and stepping to a position that’s a percentage of the way along the path, controlled by a slider that goes from 0 (home) to 1 (door).