How To Do Lerping – Unity C#

Lerping is one way to get an object to move toward another, although a simpler method is Vector.MoveTowards which I explain here. Lerping is good to know though and has other uses such as gradually changing one value into another. It’s easy to do wrong, so I’ll talk about the right way if you want to get a constant speed.

I was using lerping in an earlier prototype of Puzzledorf to make pieces slide across the board, although in the end I decided snappy movements worked better for a grid-like puzzler – it helps the game to keep up with your brain so you can try lots of solutions quickly. So if you use lerping, think about the speed of it and trial it.

Lerp

Psuedo code:

progress += deltaTime;
Object Lerp( currentPosition, endPosition, progress);

When you lerp, you don’t pass in a speed. Instead you pass in progress, also often written as t but that sounds more cryptic.

What is progress? It’s a variable that represents how close the object is to the final position. If progress is 0.1, it’s 10% of the way there. If it’s 0.2, it’s 20% of the way there.

So Lerp is saying, “Move from my current position to the end position, and here’s how much progress I’ve made so far”.

Why do we add delta time to progress? Because that makes sure you’re always incrementing it by a tiny amount based on how much time has elapsed each frame.

Example code:

 // Normalized progress from 0-1 that represents movement from start to end
 progress = Mathf.Clamp01(progress + Time.deltaTime);
 // move this objects position = Lerp(starting position, lerp to this position, point of movement along timeline);
 piece.transform.position = Vector3.Lerp(piece.transform.position, end, progress);

That’s just a quick example of how we would modify progress, and then apply it to the lerp.

We clamp progress to make sure it doens’t go beyond a value of 1. This ensures it always arrives at it’s destination. Without clamping, it may never completely arrive at the destination. Increasing progress in this way also ensures that our speed is constant and linear.

Here’s an example program:

public GameObject block; 

void Update () 
{     
    if (Input.GetKeyDown(KeyCode.Space))     
    {
         StartCoroutine(MovePieceLerp(block, block.transform.position + new Vector3(10, 0, 0)));
    } 
}

IEnumerator MovePieceLerp(GameObject piece, Vector3 end)
 { 
    float progress = 0;
    while (progress < 1)
    {
       // Normalized progress from 0-1 that represents movement from start to end
       progress = Mathf.Clamp01(progress + Time.deltaTime * 10);
       // move this objects position = Lerp(starting position, lerp to this position, point of movement along timeline);
       piece.transform.position = Vector3.Lerp(piece.transform.position, end, progress);
       yield return null;
    } 
 }

We have a public game object called block. Pass any object you like in the inspector.

On Update, we start a Coroutine when we press the Space key. We pass the block game object in and set it’s end destination. Currently it will move 10  units to the right.

The co-routine keeps running until the object has finished moving.

Every loop of the coroutine, we add to progress to measure the progress to our final destination. We then move the object’s position a little more, and return null, because co-routines need to return a value.

Try it out. If you are still a bit unsure about Lerping, try looking at the Unity documentation here.

If you enjoyed reading, try my game Puzzledorf out on Steam and Humble Bundle.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: