Get function of curve for fancy lerping

Hey,

I was wondering what the easiest way to find the function for a graph like this would be:

As you can see, it has a sort of “wobble” at the end where it overshoots 1 and then bounces back. Currently I am using a somewhat messy solution as shown below. I would imagine this curve would have a pretty simple function if I could figure it out :stuck_out_tongue:

            // start and end values
            float startSize = 1;
            float endSize = 5;
            float duration = .75f;

            float t = 0f;
            float newScale = 0f;

           // SmoothStep through each peak/valley
            while (t < 1)
            {
                t += Time.deltaTime / (duration * .5f);
                newScale = Mathf.SmoothStep(startSize, endSize * 1.2f, t);
                transform.localScale = new Vector3(newScale, newScale, newScale);
                yield return null;
            }
            t = 0f;

            while (t < 1)
            {
                t += Time.deltaTime / (duration * .2f);
                newScale = Mathf.SmoothStep(endSize * 1.2f, endSize, t);
                transform.localScale = new Vector3(newScale, newScale, newScale);
                yield return null;
            }
            t = 0f;

            while (t < 1)
            {
                t += Time.deltaTime / (duration * .15f);
                newScale = Mathf.SmoothStep(endSize, endSize * 1.14f, t);
                transform.localScale = new Vector3(newScale, newScale, newScale);
                yield return null;
            }
            t = 0f;

            while (t < 1)
            {
                t += Time.deltaTime / (duration * .15f);
                newScale = Mathf.SmoothStep(endSize * 1.14f, endSize, t);
                transform.localScale = new Vector3(newScale, newScale, newScale);
                yield return null;
            }

Not sure why you would even do it this way. You can add an AnimationCurve field to your class, draw the curve you want in the inspector, and then just sample it at different t values.

Check out the docs on Curves, use the Evaluate function to get the value of Y at X

1 Like

@skalev - thanks! you saved me countless hours of trying to brute force this. I should have remembered the curve field, so simple.

Berp function here: http://wiki.unity3d.com/index.php?title=Mathfx

–Eric

This looks really cool! I’ll have to experiment with this. Thanks!

I tried the Mathfx.Berp function but it wasn’t pronounced enough. Very close…

You can alter the function to make it more pronounced, such as multiplying by 3.5f instead of 2.5f.

–Eric