Home > Software design >  How to convert lerp to cubic easing in?
How to convert lerp to cubic easing in?

Time:09-22

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);
        }

CodePudding user response:

You should probably just use it in place of your fraction in the Vector3.Lerp.

Lerp is just the linear interpolation of vectors, i.e. 0.3 means 30% from one and 70% from the other. If we want non linear interpolation we can just apply some arbitrary function to the input value. For example, that 0.3 may be transformed to 0.027, and that can then be used as the input to Lerp to get 2.7% / 97.3% blend instead.

CodePudding user response:

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.

  • Related