How to lerp in Unity.

At some point of game development all of us have a need to slowly move an object from one position to another or slowly rotate an object. Unity has an inbuilt function for that. It’s called Lerp. Lerp basically means linear interpolation. Lerp is majorly used with position and rotation. In all other cases its used with mathf to calculate the interpolation value. In this tutorial, we will see how to use lerp function in Unity for your game.

Moving an object slowly Using Vector3.Lerp

In Unity lerp function of the 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 can be modified to change the position slowly in the update function.

Default lerp syntax

Vector3.Lerp(Vector3 start, Vector3 end, float time_passed/duration);
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 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;
        
    }
}

Rotating an object slowly Using Quaternion.Lerp

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

Unity quaternion lerp output

Here is the code for 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;
        
    }
}

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;
    }
}

Move slowly in a curved way using Vector3.Slerp

Unity Slerp output

This is not 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 the space.

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;
        
    }
}

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;
    }
}

Slowly Zoom camera using 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

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 alternatives

Using 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 question 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.