AnimationCurve.Evaluate - Get time by value

David Horák picture David Horák · Aug 27, 2014 · Viewed 8.3k times · Source

Is there a build-in way how to get a time by value from Animation curve in Unity3d? (The opposite way of Evaluate)

I need to achieve this (instead of getting value from time):

float time = AnimationCurve.Evaluate(float value);

Generically speaking getting X value from Y value.

Answer

user2330450 picture user2330450 · Sep 1, 2017

I know this is 3 years old, but I found via a Google search, and in case someone else lands here:

I simply create an inverse curve, which allows me to look up by time.

public AnimationCurve speedCurve;

private AnimationCurve inverseSpeedCurve;

private void Start()
{
    //create inverse speedcurve
    inverseSpeedCurve = new AnimationCurve();
    for (int i = 0; i < speedCurve.length; i++)
    {
        Keyframe inverseKey = new Keyframe(speedCurve.keys[i].value, speedCurve.keys[i].time);
        inverseSpeedCurve.AddKey(inverseKey);
    }
}