Linear Interpolation — Unity
In the previous article, I wrote about the Look At function in Unity.
This article will look into Linear Interpolation, also known as Lerp.
What is Lerp?
Lerp is an in-between two points calculation that continues until the destination is met.
For example, 1,2,3. 50% of 3 is 1.5 (2) half way in-between 1 and 3.
Lerping is good for light bulbs or lighting in general, as a half opacity light emission can lerp from 50% to full emission 100% overtime.
Additionally, Lerp is also a good function for moving to one place to another overtime.
Lerp works best within the Update function call.
First, I’ll begin with setting up the variables for the lerp code.
Within the Update function I’ll use the Input keys R and G to lerp from.
When I press the r key, I’ll set the isRed Boolean to true, and ensure the other values are false.
After the r key is confirmed, I’ll check for isRed as a good practice and set the lerp value for the red color.
Get Zac Bogner’s stories in your inbox
Join Medium for free to get updates from this writer.
The current light color by default will be white, where this will be the starting point to lerp from.
Using the Color.Lerp function, this is the primary method to transition from the current value to the next.
Mathf.PingPong will take in the time value and increase and decrease the time property in seconds, followed by a length.
And here, I’ll test the Red Color Lerp. The Color Lerp Red will transition from white to red.
Next, the code for the Green Color Lerp is basically the same, just switching the color variables defined.
Here is the Color Green Lerp interaction when pressing the G key.
And finally, here’s toggling through color lerp R and color lerp G for linear interpolation (lerp).
And that's all for Linear Interpolation (Lerp) in Unity.
Stay tuned for more.
Thanks,
Zac B.










