Description
The LerpTo
method performs linear interpolation between two floating-point values, from
and to
, based on a given fraction frac
. The interpolation can be optionally clamped to ensure the result stays within the range defined by from
and to
.
Usage
Use the LerpTo
method when you need to smoothly transition between two values over a specified fraction. This is particularly useful in animations or gradual changes in game states.
The method signature is:
public static float LerpTo(float from, float to, float frac, bool clamp)
Parameters:
from
: The starting value of the interpolation.
to
: The ending value of the interpolation.
frac
: A value between 0 and 1 representing the interpolation fraction. A value of 0 returns from
, and a value of 1 returns to
.
clamp
: If true
, the result is clamped between from
and to
.
Example
// Example usage of LerpTo
float startValue = 0.0f;
float endValue = 10.0f;
float fraction = 0.5f;
bool shouldClamp = true;
float result = MathX.LerpTo(startValue, endValue, fraction, shouldClamp);
// result will be 5.0f if clamped, otherwise it will be the interpolated value based on the fraction.