Is there a way I can create my own ease type when using lerp? So, I apply a lerp position to a transform.position. Now, is there a way I can customize the ease type?
Edit, it does not need to be Lerp, but some function to move a position.
Is there a way I can create my own ease type when using lerp? So, I apply a lerp position to a transform.position. Now, is there a way I can customize the ease type?
Edit, it does not need to be Lerp, but some function to move a position.
I found this…
But am unsure how I would apply it to a position transition.
Any help would be appreciated.
Thank you.
use itween, leantween, dotween, or GitHub - rakkarage/Ease: Simple Unity3D Easing
Well, to be honest, my concern was setting a new tween each input. Suppose I can test it. I am building for mobile so, I was concerned about performance. It’s probably safe tho.
Thanks.
I find the built-in AnimationCurve to be particularly useful for this.
Here’s a bunch of the actual formulas for easing:
they are all shaped like this delegate:
public delegate float Ease(float current, float initialValue, float totalChange, float duration)
I include in it a util method to shape it like the standard lerp:
public static float EasedLerp(Ease ease, float from, float to, float t)
{
return ease(t, from, to - from, 1f);
}
Used like:
var result = EaseMethods.EasedLerp(EaseMethods.CubicEaseIn, fromValue, toValue, t);
Note, this assumes you are increasing the value from 0->1, not passing in increments each frame. Passing in Time.deltaTime will not work here.
Also, you’ll notice towards the bottom of it, I have util methods for Vectors and Quaternions.
To use it over a duration of time you’d do something like this:
public IEnumerator MyCoroutine()
{
float t = 0f;
float dur = 10f;
Vector3 start = this.transform.position;
Vector3 end = start + *some change we want to change*;
while(t < dur)
{
this.transform.position = EaseMethods.EaseVector3(EaseMethods.CubicEaseIn, start, end, t, dur);
t += Time.deltaTime;
yield return null;
}
}
Though really, a tween engine is probably better than the coroutine here. Which the rest of that namespace in the link is (SPTween is my implementation of tweening). And of course, as was previously suggested iTween, HOTween, DOTween, etc.