Our previous post about genetic algorithms in games: Genetic algorithm in games may seem quite difficult (but it’s still worth to read it!) so now it’s time to back to basics. Today we are going to talk about using Lerp functions in Unity games. We prepared a little elaboration how to use them properly and how they can be modified to create custom interpolation functions.
Lerp – introduction
As mentioned above, Lerps are linear interpolation functions. They use parametrized linear function to “connect” start and final values. You can find Lerp functions in:
- Mathf class – Mathf.Lerp(float a, float b, float t) – it uses linear function to interpolate between a and b with parameter t. It also implements saturation so if t is lower than 0 it return a value. If t is higher than 1 it return b.
- Vector3 class – Vector3.Lerp(Vector3 a, Vector3 b, float t) – it makes everything that Mathf.Lerp makes but for all Vector3 components.
- Color3 class – Color.Lerp(Color a, Color b, float t) – the same situation as with the Vector3 – you can interpolate between two colors.
Lerps are perfect tool to create simple animations with moving objects or changing colors.
It should also be mentioned that if we want to create time dependent animation with Lerp we have to use time scaling. To see how to achieve this, look at code snippet below:
1 2 3 4 5 6 |
float start, end; float lerpStartTime, lerpDuration; void Update () { float newValue = Mathf.Lerp (start, end, (Time.time - lerpStartTime) / lerpDuration)); } |
Improved interpolations
Lerp uses linear functions but what if we want to make the transitions smoother? What if we want to replace linear transformation with more sophisticated function? The answer is simple. We can prepare simple functions which have the same functionality as Mathf.Lerp but which work in more elegant way. Take a look at this article: Smoothstep. It decribes a more complex approach. We can connect two points with polynomial function with higher order than 1 to fulfill some additional conditions. Linear interpolation provides nonsmooth transition with nonzero derivatives in limit points. If we want to create smooth moving animation we want to get zero speed (first derivative) in both start and end point. To make it even more smooth we can use 5th order (or higher but with 5th order we can fulfill zero speed and zero acceleration conditions).
We can also replace linear function with other functions, for example with sine function. Just look at the picture below to see how these all functions look like:
Remember, these functions take values from 0 to 1 so there is a need to scale them as in the code below:
1 2 3 4 5 |
public float SmoothStep (float start, float end, float t) { float clampedT = Mathf.Clamp01 (t); return (end - start) * (-2f * Mathf.Pow (clampedT, 3f) + 3f * Mathf.Pow (clampedT, 2f)) + start; } |
Lerp – examples
We prepared a simple example which implements interpolations for moving objects and for changing button image color. In the scene you can see four spheres. There is also Lerps script which creates a simple spheres animation by moving them from point A to point B. Each ball has different interpolation function so you can see a different kind of move (functions from top ball to bottom: linear, SmoothStep, SmootherStep, sine interpolation). To reset animations just click reset button which contains Color.Lerp function:
As usual, you can download package with scene and scripts from here: Download
Pingback: How to Lerp properly?! - Aliasing Games()
Pingback: SmoothDamp - dynamic smoothing! - Aliasing Games()
Pingback: Extension methods in C# and Unity - Aliasing Games()
Pingback: Partial class and Unity - Aliasing Games()