Welcome to this Unity 2D Lerp Tutorial. The Unity 2D Lerp function is often used to move objects along a path or to change values along a path.

However in essence LERP is code word for Linear Interpolation, but what is linear interpolation?

Well interpolation is a mathematical concept which is used to fit points within other points.

Essentially how this works is it takes a set of points and estimates other points for a given x or y value on a graph.

Visually explaining Lerp

Here is essentially what this would look like.

To explain this in brief. The green dots are all the co ordinates that are known.

The red one is a estimation of what the point will look like at that given x or y. So how Lerp works in unity is it will use the first point you give it and the 2nd point you give.

It and create estimations for the time value you pass in. By doing so you can get linear values back between the point 1 and point 2.

With these values you can modify scales, object positions etc, anything that has a linear motion or change.

Here is what the Lerp function definition looks like.

public static Lerp(Vector2 a, Vector2 b, float t);

So in terms of our graph Vector2 a could be the first green dot at the bottom of our graph and Vector2 b could be our last one at the top.

The float t, will move us along on the x axis and give us estimates of the line drawn between Vector a and Vector b.

In so doing if we step the time in our Lerp function we would be able to animate, move or modify unity game object.

I hope this makes sense. Can be quite tricky without a mathematical background.

You might begin to understand it more in depth with the following demonstrations.

Demonstrating unity lerp between two positions

Let’s start off with a new unity project. Open up unity hub and create a project called unity 2d lerp tutorial.

So generally when we say translate in the same sentence as lerp we talking about different things really.

Lerp will give us the next co ordinate in our series, where translate will add values to our vector.

It technically would be possible to simulate a lerp with translate. By using a linear function to determine the gradient of our linear graph.

However let us stick to the lerp way of doing a “translate”. This will be using the time function. Let’s start by adding a basic square to our scene.

So in the assets tab add a square 2d sprite like this.

Drag it into your hierarchy.

Let’s now create the first basic c# script to handle a “translate”. Go ahead and right click in the assets tab and create a new c# script and call it translate lerp.

Open that up in visual studio code. Here is what our code is going to look like.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class TranslateLerp : MonoBehaviour { // Start is called before the first frame update float accum = 0.0f; Vector2 p1, p2; void Start() { p1 = new Vector2(0, 0); p2 = new Vector2(20, 20); } // Update is called once per frame void Update() { accum += 1.1f * Time.deltaTime; this.transform.position = Vector2.Lerp(p1,p2, accum); } }

Save that off and attach that to our square by dragging it into the insepector.

Run that and you should get a square moving on the screen. So very simple how this works.

First we have our two vectors point 1 and point 2. We then lerp between the two passing the lerp function our deltaTime * 1.1f. The 1.1f which is our speed at which we want to move our object.

We assign this to a accum variable which is our lerp float over time.

Modifying this code to lerp scale

All we now need to do to get unity lerp to scale is change the code to have this instead.

this.transform.localScale = Vector2.Lerp(p1,p2, accum);

Unity lerp smooth step

Want to smooth out the motion try this.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class TranslateLerp : MonoBehaviour { // Start is called before the first frame update float accum = 0.0f; Vector2 p1, p2; void Start() { p1 = new Vector2(0, 0); p2 = new Vector2(20, 20); } // Update is called once per frame void Update() { accum += 1.1f * Time.deltaTime; this.transform.position = Vector2.Lerp(p1,p2, Mathf.SmoothStep(0,1,accum)); } }

Here we use Mathf.SmoothStep to control the estimation.

That’s it for this tutorial. Going to end it here and in the next section I will be answering some frequently asked questions.

How do I do a smooth lerp in unity? To do a smooth lerp you can use a smoothing function like Mathf.SmoothStep.

How do I do lerp on scale in unity? To lerp on scale in unity you can simply provide 2 vectors and lerp over the localScale like this.

accum += 1.1f * Time.deltaTime;

this.transform.localScale = Vector2.Lerp(p1,p2, accum);

How do I do a translate lerp in unity? You can lerp between two points like this.

accum += 1.1f * Time.deltaTime;

this.transform.position= Vector2.Lerp(p1,p2, accum);

Some final words

I hope this tutorial was useful and helped you understand lerp in a new way. If you want to support me and my content please check out my skillshare course here:

https://skl.sh/2YhzEfe

If you prefer watching video over reading tutorials, why not subscribe to my YouTube channel over here: https://www.youtube.com/channel/UC1i4hf14VYxV14h6MsPX0Yw?sub_confirmation=1

Here are also some other tutorials you might be interested in: