Skip to content

Unity Lerp: How to Use Lerp in Unity With Examples

Unity Lerp: How to Use Lerp in Unity

Unity Lerp is used to smoothly interpolate between two values. It is commonly used for object movement, camera transitions, color changes, rotation, UI animations, and many other effects in Unity.

If you have ever wanted to move an object smoothly from one position to another, gradually change a color, or create a simple transition, Lerp can make the process much easier.

In this guide, you’ll learn how Unity Lerp works, how to use Mathf.Lerp, Vector3.Lerp, Color.Lerp, and Quaternion.Lerp, and how to control Lerp over time with simple C# examples.

Understanding Unity Lerp for Smooth Transitions
Understanding Unity Lerp for Smooth Transitions

What Is Lerp in Unity?

Lerp stands for Linear Interpolation. It calculates a value between a starting value and an ending value based on an interpolation value called t.

The basic formula is:

result = start + (end - start) * t;

The t value normally goes from 0 to 1.

For example:

  • t = 0 returns the starting value.
  • t = 0.5 returns a value halfway between the start and end.
  • t = 1 returns the ending value.

Unity provides different Lerp functions depending on the type of value you want to interpolate.

Some commonly used functions are:

  • Mathf.Lerp for float values
  • Vector2.Lerp for Vector2 values
  • Vector3.Lerp for positions and other Vector3 values
  • Color.Lerp for color transitions
  • Quaternion.Lerp for rotations

Let’s look at how they work with practical examples.

How Does Unity Lerp Work?

The basic syntax of Mathf.Lerp is:

Mathf.Lerp(a, b, t);

Here:

  • a is the starting value.
  • b is the target value.
  • t controls how far the interpolation has progressed.

For example:

float startValue = 0f;
float endValue = 10f;
float t = 0.5f;

float result = Mathf.Lerp(startValue, endValue, t);

Debug.Log(result);

The result will be:

5

because 0.5 represents the halfway point between 0 and 10.

What Does the t Parameter Mean in Unity Lerp?

The t parameter is one of the most important parts of Lerp.

Consider this:

Mathf.Lerp(0f, 100f, t);

The result changes depending on the value of t.

t value Result
0 0
0.25 25
0.5 50
0.75 75
1 100

So, if you want the result to move from the start value to the target value, you generally need to gradually change t from 0 to 1.

Unity’s Mathf.Lerp also clamps t to the 0–1 range.

This is important because Lerp does not automatically understand seconds. The t value represents how far between the two values you want to interpolate.

How to Use Mathf.Lerp in Unity

Mathf.Lerp is useful when you want to interpolate between two numbers.

For example, you can gradually change a value from 10 to 50:

float currentValue = Mathf.Lerp(10f, 50f, 0.5f);

Debug.Log(currentValue);

The result will be 30.

You can use this technique for things such as:

  • Health bars
  • Volume
  • UI values
  • Speed
  • Camera settings
  • Other numeric properties

How to Use Vector3.Lerp in Unity

One of the most common uses of Unity Lerp is moving a GameObject between two positions.

For this, we can use Vector3.Lerp.

Vector3 startPosition = new Vector3(0, 0, 0);
Vector3 endPosition = new Vector3(10, 0, 0);

float t = 0.5f;

transform.position = Vector3.Lerp(
    startPosition,
    endPosition,
    t
);

When t is 0.5, the object will be halfway between the two positions.

Move an Object Over Time Using Vector3.Lerp

To make the movement happen over a specific amount of time, we can calculate t using elapsed time.

public Vector3 startPosition;
public Vector3 endPosition;
public float lerpDuration = 3f;

private float elapsedTime = 0f;

void Update()
{
    elapsedTime += Time.deltaTime;

    float t = elapsedTime / lerpDuration;

    transform.position = Vector3.Lerp(
        startPosition,
        endPosition,
        t
    );
}

Here, Time.deltaTime increases elapsedTime every frame.

The following line converts the elapsed time into a value between approximately 0 and 1:

float t = elapsedTime / lerpDuration;

If the duration is 3 seconds:

0 seconds → t = 0
1 second  → t = 0.33
2 seconds → t = 0.66
3 seconds → t = 1

This allows the object to move from the starting position to the target position over the specified duration.

How to Use Color.Lerp in Unity

Lerp can also be used to create smooth color transitions.

Unity provides Color.Lerp for this purpose.

public Color startColor = Color.red;
public Color endColor = Color.blue;

private float elapsedTime = 0f;
public float duration = 2f;

private Renderer objectRenderer;

void Start()
{
    objectRenderer = GetComponent<Renderer>();
}

void Update()
{
    elapsedTime += Time.deltaTime;

    float t = elapsedTime / duration;

    objectRenderer.material.color = Color.Lerp(
        startColor,
        endColor,
        t
    );
}

This gradually changes the object’s color from red to blue.

You can use the same idea for:

  • Damage effects
  • UI color changes
  • Highlight effects
  • Environment transitions
  • Visual feedback

Also check:

How to Use Quaternion.Lerp in Unity

Lerp can also be used with rotations.

Unity provides Quaternion.Lerp for interpolating between two rotations.

public Quaternion startRotation;
public Quaternion endRotation;

public float rotationDuration = 1f;

private float elapsedTime = 0f;

void Update()
{
    elapsedTime += Time.deltaTime;

    float t = elapsedTime / rotationDuration;

    transform.rotation = Quaternion.Lerp(
        startRotation,
        endRotation,
        t
    );
}

This allows an object to gradually change from its starting rotation to the target rotation.

For some rotation-based effects, Quaternion.Slerp may also be a better choice, especially when you want spherical interpolation.

Common Unity Lerp Mistake

One common mistake is assuming that Lerp automatically creates a fixed-duration movement.

For example:

transform.position = Vector3.Lerp(
    transform.position,
    target.position,
    0.1f
);

When this runs every frame, the object repeatedly moves a percentage of the remaining distance toward the target.

It can look smooth, but it is different from moving between two fixed positions over a known duration.

For a controlled duration, it is usually better to calculate t from elapsed time:

elapsedTime += Time.deltaTime;

float t = elapsedTime / duration;

transform.position = Vector3.Lerp(
    startPosition,
    targetPosition,
    t
);

This gives you much better control over how long the transition takes.

Unity Lerp vs MoveTowards

Lerp and MoveTowards are both useful for moving values, but they work differently.

Lerp MoveTowards
Interpolates between two values Moves toward a target
Uses a t value Uses a maximum movement amount
Useful for timed transitions Useful for constant-speed movement
Common for smooth transitions Common for gameplay movement

For example, if you want an object to reach a target in exactly 2 seconds, Lerp can be a good option.

If you want an object to move toward a target at a fixed speed, MoveTowards may be more suitable.

When Should You Use Unity Lerp?

There are many situations where Lerp can be useful in a Unity game.

Some common examples include:

  • Moving an object between two positions
  • Smooth camera transitions
  • Changing object colors
  • UI animations
  • Changing values gradually
  • Scaling objects
  • Rotating objects
  • Creating simple visual effects

The important thing to remember is that Lerp itself is simply calculating a value between two points. How you change t determines how that transition behaves.

How to Master in Unity Lerp Function
Unity Lerp Function – Detailed explain

Frequently Asked Questions About Unity Lerp

What is Unity Lerp?

Unity Lerp is a linear interpolation technique used to calculate a value between a starting value and a target value. It is commonly used for movement, colors, rotation, UI and other transitions.

What does t mean in Unity Lerp?

t controls the interpolation amount. A value of 0 represents the start, 0.5 represents the middle, and 1 represents the target.

What is the difference between Mathf.Lerp and Vector3.Lerp?

Mathf.Lerp is used for numeric float values, while Vector3.Lerp is used to interpolate between two Vector3 values, such as object positions.

Can I use Lerp to move an object over time?

Yes. You can calculate t using elapsed time and a duration, then pass that value to Vector3.Lerp.

float t = elapsedTime / duration;

transform.position = Vector3.Lerp(
    startPosition,
    targetPosition,
    t
);

Is Lerp the same as MoveTowards in Unity?

No. Lerp calculates a value between two points based on t, while MoveTowards moves toward a target by a specified amount. Choose the one that fits the type of movement you need.

Conclusion

Unity Lerp is a simple but useful tool for creating controlled transitions in your game. Once you understand the start value, target value and t parameter, you can use Lerp for many different tasks.

Mathf.Lerp is useful for numbers, Vector3.Lerp can be used for positions, Color.Lerp handles color transitions, and Quaternion.Lerp can be used for rotations.

The most important thing is to understand how the t value changes over time. Once you get that part right, using Lerp in your Unity projects becomes much easier.

Try using Lerp in a small movement or color transition first, then experiment with it in your camera, UI and gameplay systems.

Leave a Reply

Your email address will not be published. Required fields are marked *