Linear Interpolation: Difference between revisions
(Created the page) |
-u ndefined- (talk | contribs) m (Text replacement - "Category:Scripts" to "Category:Scripting") |
||
Line 32: | Line 32: | ||
As a convenient script to interpolate between values non-linearly (for example, make it slowly gain speed at the start and reduce speed at the end, which is more natural for animations), you can use [https://play.fancade.com/5E6AB5ADF3B3BC2F Easings] by [[Sounak9434]]. | As a convenient script to interpolate between values non-linearly (for example, make it slowly gain speed at the start and reduce speed at the end, which is more natural for animations), you can use [https://play.fancade.com/5E6AB5ADF3B3BC2F Easings] by [[Sounak9434]]. | ||
[[Category: | [[Category:Scripting]] |
Revision as of 07:12, 16 June 2024
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

And here is LERP for vectors:

Examples

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.

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.
As a convenient script to interpolate between values non-linearly (for example, make it slowly gain speed at the start and reduce speed at the end, which is more natural for animations), you can use Easings by Sounak9434.