# How to lerp in Unity.

Contents

## 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.

Lerp is majorly used with position and rotation. In all other cases it’s used with mathf to calculate the interpolation value. In this tutorial, we will see how to use the lerp function in Unity for your game.

## Moving an object slowly Using Vector3.Lerp (Lerp position)

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

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;

}
}

## Rotating an object slowly Using Quaternion.Lerp (Lerp rotation)

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.

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;

}
}

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

## Lerp Vs Slerp example

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.

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 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 ( 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

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

}
}

## 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 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.

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.

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