Unity Linear Interpolation

Unity Linear Interpolation tutorial with examples

Linear interpolation has many uses. I’ll try to cover the most  useful ones. This paragraph may be confusing, but everything will be explained in details later. Important thing to point out at the beginning, online tutorial tend to have two different approaches to this subject. First, like in official video, shows example of Lerp() function which is not linear. Another approach is to use percentage as time parameter. Lerp() can be used together with Vector3 and Math class. It’s common to use Math.Lerp() to change parameters like light intensity, colour or sprite transparency. While Vector3 is responsible for 3d movement. Lets start with example of changing light intensity in non linear way using Lerp()function.

Math.Lerp()

The Mathf.Lerp function takes 3 float parameters: one representing the value to interpolate from; another representing the value to interpolate to and a final float representing how far to interpolate. In this case, the interpolation value is 0.5 which means 50%.

float result = Mathf.Lerp (0f, 8f, 0.5f);

This one returns 4, (0+8)*0.5.

Lets take a look at mentioned earlier example

using UnityEngine;
using System.Collections;

public class LerpTutorial : MonoBehaviour {
 public Light light2;

 void Start () {
//We set intensity of light to 0
 light2.intensity = 0f;
 }
 
 void Update () {
//Two ways of changing the last parameter
 //light2.intensity = Mathf.Lerp(light2.intensity, 8f, 0.01f );
 light2.intensity = Mathf.Lerp(light2.intensity, 8f, 0.01f*Time.deltaTime);

 }
}

This one is changing the intensity of light, of course. As a time parameter we can pass the float value, what  changes the value every frame. Float value should be very small to notice the difference. 8f is maximum parameter for intensity, so don’t try putting larger values there, or do it at your own risk. Other way is to multiply the value by the Time.delta time what makes it change every second instead of every frame. To speed up the process I incensed the float value 10 times.

So this is an example of non linear transition. Each time call this method the minimum value is different. Last frame output becomes next frame input. It reminds me about recursive functions. You can learn more about them here. this way result if this method will slowly  approach the maximum value which in not linear.

Vector3.Lerp();

To illustrate linear transition lets use Vector3 class. Interpolates between the vectors a and b by the interpolant t. The parameter t is clamped to the range [0, 1]. This is most commonly used to find a point some fraction of the way along a line between two endpoints (e.g. to move an object gradually between those points). Same thing as Math class but with vectors. Takes X of a and b vectors and return point between them, the same for rest of axis.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LerpTutorial : MonoBehaviour {
 public float timeTakenDuringLerp = 1f;
 private bool isLerping;
 private Vector3 startPosition;
 private Vector3 endPosition;
 private float timeStartedLerping;

 void Update()
 {
 isLerping = true;
 timeStartedLerping = Time.time;
 startPosition = transform.position;
 endPosition = new Vector3(0, 0, 0);
 }

 void FixedUpdate()
 {
 if (isLerping)
 {
 float timeSinceStarted = Time.time - timeStartedLerping;
 float percentage = timeSinceStarted / timeTakenDuringLerp;
 transform.position = Vector3.Lerp(startPosition, endPosition, percentage);

 if (percentage >= 1.0f)
 {
 isLerping = false;
 }
 }
 }
}

This way we have fixed start and end position, and we change the value of the time based on the Time.deltatime. And we execute it FixedUpdate() which means every second.

This process will result in linear transformation.

You can experiment with time value by setting it to something different than constant.

float percentage = timeSinceStarted / timeTakenDuringLerp;
percentage = Mathf.Sin(t * Mathf.PI * 0.5f);

This will produce totally different result. I encourage you to try this method using function of time as a parameter.

Both ways of using this function can be useful, we can change the start parameter or time over time. Each way we have slightly different result. Lerp() method can be called not only from Update method using Coroutines. I wrote article about it here. Stay Awesome!

Author: indiedevart

Indie Games Developer

One thought on “Unity Linear Interpolation”

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