Search code examples
c#unity-game-engineeasinglerp

How to convert lerp to cubic easing in?


I need to convert the following code from using lerp to using cubic easing in, but I don't know what to do with the returned float value from the cubic easing in function, because I'm using Vector3 instead of floats. Can someone please give me a hint?

    if (Vector3.Distance(activeTween.Target.position, activeTween.EndPos) > 0.1f)
        {
            float fraction = (Time.time - activeTween.StartTime) / activeTween.Duration;
            activeTween.Target.position = Vector3.Lerp(activeTween.StartPos, activeTween.EndPos, fraction);
        }

Solution

  • You need a cubic function that is 0 at the start and 1 and the end. I would try something like:

    public float EaseIn(float t)
    {
        return t * t * t;
    }
    
    if (Vector3.Distance(activeTween.Target.position, activeTween.EndPos) > 0.1f)
    {
        float fraction = (Time.time - activeTween.StartTime) / activeTween.Duration;
    
        activeTween.Target.position = Vector3.Lerp(activeTween.StartPos, activeTween.EndPos, EaseIn(fraction));
    }
    

    Assuming that fraction was already going from 0 to 1.