In part 6 of our multi-part game audio programming series, Brennan shows us how to use lerping within a game. We’ve included a list of programming terms used in this video below, to help you follow along.
Fill out the form to download this Unity project, as well as a tutorial project here:
Lerping - No, this is not a misspelling of LARPING (Live action role-playing). Lerping is a shortening of “Linear Interpolation”. Basically it is a built-in function in Unity’s C# that calculates a point between point a and point b, by t. That’s a little confusing, so here is a real world example, then a number example. Say you have a hallway. Each end has a room, and each room is a point, and you want to get to room b. You start at point a, and every time you take a step you are moving a certain distance towards point b. That distance is t. You will have to take multiple steps with a distance of t to reach your desired point. The Lerp function is behaving similarly. It simply does a bit of math to give you a point on a line that you will be at after taking one step, that step being t. Here is the same example with numbers. You have a line with point a being 0, and point b being 1. You give the lerp function a t value of 0.5. Where will the function say you are after one step? Halfway between 0 and 1, or 0.5. That would look like this: Lerp(0, 1, 0.5) = 0.5. So how does this help us move along a line in multiple steps if this function will give us the same value every time? Simple, we change t. We add the previous value of t to the current value of t. We have already moved 0.5 down the line, so we will add another 0.5 to that. Now we have Lerp(0, 1, (0.5 + 0.5)) = 1. Now we have reached the end of the line! This is very useful for things like fading audio in and out. But, what if we don’t want a constant, linear movement between the two points? Well, you could look up different easing functions to get different kinds of curves. I.e. logarithmic, exponential, etc.