Skip to main content

LerpingValue<T>

An abstract class in Root.Township, Alta.Utilities.

Base for a value that eases from where it is to a target over time. Call Update each frame and it pushes every step through the setter given to the constructor; subclasses supply the maths.

Constructors​

MemberWhat it is
LerpingValue(Action<T> setValue)Creates a LerpingValue from setValue.

Fields​

6 private and internal fields
MemberWhat it is
private T currentBacking field for Current.
private Func<T> getTargetReturns the target. Called every frame, so the target can move during the lerp.
private T initialThe value the running lerp started from.
private float lerpSpeedHow long a lerp takes, in seconds, despite the name (default 1).
private float progressLerp progress from 0 to 1.
private Action<T> setValueSetter that receives each new value.

Properties​

MemberWhat it is
T Current { get; }The value last pushed to the setter.
bool IsLerping { get; private set; }True while a lerp is running.

Methods​

MemberWhat it is
void Kill()Stops the running lerp where it is.
void Lerp(T target, float lerpSpeed)Starts a lerp from the current value to a fixed target over lerpSpeed seconds, or to a target read each frame. The protected overload does the interpolation.
void Lerp(Func<T> getTarget)Starts a lerp from the current value to a fixed target over lerpSpeed seconds, or to a target read each frame. The protected overload does the interpolation.
void Snap(T newTarget)Jumps straight to a value, pushes it to the setter and stops any lerp.
void SnapIfNotLerping(T newTarget)Calls Snap only when no lerp is running.
void Update()Advances the lerp by frame time and pushes the new value. At the end it lands on the target and stops.
protected abstract T Lerp(T initial, T target, float progress)Starts a lerp from the current value to a fixed target over lerpSpeed seconds, or to a target read each frame. The protected overload does the interpolation.