Unity Lerp complete tutorial with code samples

What is Unity lerp

Lerp in Unity represents the equivalent of mathematical linear interpolation. Using lerp, you can get any point between positions, similar to the methods used in linear interpolation for curve fitting.

To understand it better Let’s say we have a position X and a position Y. Now if you need the midpoint of both the position then you can use the Unity Lerp function to and pass 0.5 as the third input to get the midpoint. Similarly, you can pass any value between 0 and 1 to get the positions between your start and end point.

Lerp is majorly used with position and rotation. In all other cases it’s used with mathf to calculate the interpolation value. Lerp in Unity can be used with Vector3, Vector2, Quaternion, Mathf , Color and Material. In order to use Lerp to slowly change from start value to end value the third input of Lerp is incremented slowly from 0 to 1.

In this tutorial, we will see how to use the lerp function in different ways in Unity for your game.

Free Unity asset banner

Unity lerp position Using Vector3.Lerp

In Unity lerp function of Unity Vector3 class takes 3 arguments. The first one is the start point, second is the end point and the third is the interpolation variable.

The third argument is modified to change the position slowly in the update function.

Default lerp syntax

Vector3.Lerp(Start position, End position, Interpolation value);
Unity vector3 lerp output

As in the above gif you can move from one position to another slowly using the lerp option. Same can be used for 2D games using Vector2.

Here is the code for the above example

using UnityEngine;

public class Unity_lerp : MonoBehaviour
{
    Vector3 start_pos;
    Vector3 end_pos;
    Vector3 ofset= new Vector3(5,0,0);
    float timer=0;
    // Start is called before the first frame update
    void Start()
    {
        start_pos=transform.position;
        end_pos=transform.position+ofset;
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position=Vector3.Lerp(start_pos,end_pos,timer/5);
        timer+=Time.deltaTime;
        
    }
}

Unity Lerp rotation Using Quaternion.Lerp

Same as vector3, quaternion can be used to rotate an object in Unity. Let’s try to rotate a cube from the initial position to a certain angle.

Unity quaternion lerp output

Here is the code for the above example

using UnityEngine;

public class Unity_lerp : MonoBehaviour
{
    Quaternion start_pos;
    Quaternion end_pos;
    float timer=0;
    // Start is called before the first frame update
    void Start()
    {
        start_pos=transform.rotation;
        end_pos=Quaternion.Euler(90, 0, 90);
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.rotation=Quaternion.Lerp(start_pos,end_pos,timer/5);
        timer+=Time.deltaTime;
        
    }
}

Unity Color Lerp

Slowly change from one color to another using Color lerp

You can change the color of the material using Color lerp in the same way. Here is an example. Let’s change the color of the cube from black to white.

Here is the code for the above example

using UnityEngine;

public class Unity_lerp : MonoBehaviour
{
    Renderer col_rend;
    float timer=0;
    void Start()
    {
        col_rend=GetComponent<Renderer>();
        
    }

    // Update is called once per frame
    void Update()
    {
        col_rend.material.color= Color.Lerp(Color.black, Color.white,timer/5);
        timer+=Time.deltaTime;
    }
}

Making an object fade using color lerp

To completely fade an object the shader rendering mode must be set to fade. This method will work with shaders with user settable rendering mode.

If it’s set to opaque your fade code will not work.

If you don’t want your object to fade completely then you can set the rendering mode as transparent.

Here is the code for the same

using UnityEngine;

public class Unity_lerp : MonoBehaviour
{
    Renderer col_rend;
    Color start_col;
    Color End_col;
    float timer=0;
    void Start()
    {
        col_rend=GetComponent<Renderer>();
        start_col=col_rend.material.color;
        End_col= start_col;
        End_col.a=0f;

        
    }

    // Update is called once per frame
    void Update()
    {
        col_rend.material.color= Color.Lerp(start_col, End_col,timer/5);
        timer+=Time.deltaTime;
    }
}

Unity mathf Lerp

Unity lerp float

To lerp a float in Unity you have to use the Mathf function. The syntax of mathf lerp is very similar to other Lerp functions in Unity.

Let’s try to increment a number from 0 to 10 using Mathf lerp.

Our Start value will be 0 and the end value will be 10. Let’s increment the interpolation value using the Unity Time function.

Here is the sample code to lerp a float from 0 to 10 in 5 seconds.

using UnityEngine;

public class Unity_lerp : MonoBehaviour
{
    float start_value=0f;
    float end_value=10f;   
    float output;


    void Update()
    {
        output=Mathf.Lerp(start_value,end_value,Time.time/5);
        Debug.Log(output);
        
    }
}

Slowly Zoom camera using Mathf.lerp

You can change the camera’s field of view using the lerp function. Alternatively, you can also move the camera towards the target using vector3.lerp

Let’s use the above example to lerp a float and change the camera’s field of view.

Here is the example code

using UnityEngine;

public class Unity_lerp : MonoBehaviour
{
    float start_value=80f;
    float end_value=50f;   



    // Update is called once per frame
    void Update()
    {
        Camera.main.fieldOfView=Mathf.Lerp(start_value,end_value,Time.time/5);
        
    }
}

Unity Lerp Vs Slerp

Unity Slerp output

This is not a Unity lerp function. It’s the cousin of lerp and it’s called Slerp. Both these functions work in the same way. The only difference is Slerp considers the vectors as direction rather than points in space. If you want to understand Slerp in more details check out Unity Documentation on Slerp

Code for the above example

using UnityEngine;

public class Unity_lerp : MonoBehaviour
{
    Vector3 start_pos;
    Vector3 end_pos;
    Vector3 ofset= new Vector3(5,0,0);
    float timer=0;
    // Start is called before the first frame update
    void Start()
    {
        start_pos=transform.position;
        end_pos=transform.position+ofset;
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position=Vector3.Slerp(start_pos,end_pos,timer/5);
        timer+=Time.deltaTime;
        
    }
}

Lerp slowing down at the end

You will not have this problem with all the above codes in this blog post but if you have taken a Lerp code online from other sources you will note that the lerp value slows down as it approaches the end.

This happens if you update the start value. The start value should remain fixed and only the interpolation value needs to change to get a linear change.

For example, in the first demo we updated the transform position based on the lerp value but we did not set the start position to transform position at every Update.

This code will not slow down

void Update()
    {
        transform.position=Vector3.Lerp(start_pos,end_pos,timer/5);
        timer+=Time.deltaTime;
        
    }

This code will slow down at the end

void Update()
    {
        start_pos=transform.position;
        transform.position=Vector3.Lerp(start_pos,end_pos,timer/5);
        timer+=Time.deltaTime;
        
    }

How to check if lerp is done in unity

If you want to do some action when the lerp is completed then you need to know if lerp is done. The best part here is, you tell Unity when lerp is done.

When the third input of lerp reaches 1 lerp is done. Most of the time we increment the timer variable using Time.deltatime. You can use an if statement to see if the timer value is equal to 1. If it’s equal to one Lerp is done.

transform.position=Vector3.Lerp(start_pos,end_pos,timer/5);
if(timer<1.01)
{
timer+=Time.deltaTime;
}
else
{
Debug.Log("lerp is done");
}

Unity lerp alternatives

Using a simple Animation

Most of the things done by Unity lerp can also be replicated using animation. If you don’t like to write code then using animation is a better option. In animation you also get the advantage of visualizing the curve. So you can smooth out the motion to make it look natural.

Using mathf.SmoothStep

Smoothstep works in the same way as lerp. The only difference is it smooths out the motion at the end points like a logarithmic graph. This makes the motion look natural compared to Unity lerp. The syntax of Smothstep is same as lerp.

Let’s try the cube movement with smoothstep. Here is the output.

moving cube using unity Smoothstep

Here is the code

using UnityEngine;
public class Unity_lerp : MonoBehaviour
{
    Vector3 start_pos;
    Vector3 end_pos;
    Vector3 ofset= new Vector3(5,0,0);
    Vector3 current_pos;
    float timer=0;
    // Start is called before the first frame update
    void Start()
    {
        start_pos=transform.position;
        current_pos= transform.position;
        end_pos=transform.position+ofset;
        
    }

    // Update is called once per frame
    void Update()
    {
        if(current_pos.x<end_pos.x)
        {
            current_pos.x=Mathf.SmoothStep(start_pos.x,end_pos.x,timer/5);
            transform.position=current_pos;
            timer+=Time.deltaTime;

        }
        
        
    }
}

Hope it was helpful. If you have any other questions regarding Unity lerp leave it in the comment section below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.