Skip to content

Instantly share code, notes, and snippets.

@drZool
Last active April 21, 2018 08:40
Show Gist options
  • Select an option

  • Save drZool/2938bc198ebccee0931d98798bd86cfa to your computer and use it in GitHub Desktop.

Select an option

Save drZool/2938bc198ebccee0931d98798bd86cfa to your computer and use it in GitHub Desktop.
This is a utility to lerp stuff over time. Feed in the time and you get normalized time and or a value of the current custom lerp.
using System;
using System.Collections;
using UnityEngine;
public struct LerpHelper<T>
{
public T from;
public T to;
public float duration;
public float start;
public LerpHelper(T from, T to, float duration, float now){
this.from = from;
this.to = to;
this.duration = duration;
this.start = now;
}
public T GetValue (Func<T,T,float, T> interp, float now, Func<float,float> TimeLerp) {
return interp(from, to, TimeLerp(GetNormalizedTime(now)));
}
public T GetValue (Func<T,T,float, T> interp, float now) {
return interp(from, to, normalizedTime(now));
}
public bool IsStarted(float now)
{
return now >= start;
}
public bool IsComplete(float now)
{
return now > start + duration;
}
[ObsoleteAttribute("Use GetNormalizedTime instead")]
public float normalizedTime(float now){
return (now - start) / duration;
}
public float GetNormalizedTime(float now){
if (duration == 0)
return 0.00001f;
return Mathf.Clamp01( (now - start) / duration );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment