Linear Interpolation

From Fancade Wiki

Linear interpolation (LERP) is a way of smoothly changing a value from one to another

The built-in LERP script does this for rotations, but there are no such scripts for numbers or vectors. This is where this pages comes along.

Here is LERP for numbers:

  • From is the initial value
  • To is the new value
  • Amount is how fast the value changes
Lerp number.png

And here is LERP for vectors:

Lerp vector.png

Examples

Lerp example1.png

Here, every frame, the block rotates 5% (0.05) of the way from its current rotation towards the goal (90°).

Note that it doesn't rotate on a constant speed, but it eases out/slows down until it comes to a stop at its goal. This is because by every frame, the From input changes itself to be the block's current rotation, which means the distance between From and To gets smaller, therefore 5% of the way towards its goal is smaller than what it was in the previous frames.

If you want the rate of change to be constant, or different, you can make the From and To constant, and change the Amount from 0 to 1 instead.

Lerp example2.png

Here, every frame, the block moves 5% (0.05) of the way from its starting position to the goal position 2,0.5,2. This time it moves at a constant rate, because the From value is only set once (using the Play Sensor). The Amount variable increases by 0.05 every frame, until it reaches 1, where it is limited by using Min.

Non-linear interpolation

It is often useful to make the movement more believable by having some acceleration (making it non-linear) without the use of physics. Apart from interpolation, this technique is sometimes called tweening or easing.

To do this, instead of using the Amount variable directly, it can be transformed first. You can raise it to some power or take a sin or use a function like smoothstep, which will make the movement smoothly accelerate and then decelerate; it looks like this:

Smoothstep.png
Smoothstep_graph.png

You can experiment with such easing functions on your own or find some elsewhere on the internet or in Fancade, such as in Easings by Sounak9434.