lerp, standing for linear interpolation, is a function in GameMaker that smoothly transitions between two values. It's commonly used in animations, movements, scaling, and various other instances where a value needs to change at a certain rate over time.
lerp function takes three arguments: the start value, the end value, and the interpolation factor (usually a value between 0 and 1). The interpolation factor determines how close the returned value is to the start or end value. An interpolation factor of 0 will return the start value, an interpolation factor of 1 will return the end value, and an interpolation factor of 0.5 will return exactly halfway between the two.
Here's the basic syntax for
For example, if you want to move an object from its current position to a new position smoothly over time, you could use
lerp like this:
In the code above,
move_speed dictates how quickly the object will move towards
target_x. Each step, the object's
x coordinate will get closer to
target_x by 5%.
lerp can sometimes never reach the end value due to the nature of floating-point precision and the fact that it's a percentage-based approach. When using
lerp for such purposes, it might be necessary to set a threshold below which you would just set the value to the end value directly to avoid an endless approach.