I'm using Unity's animation curves which unfortunately only have an Evaluate function that given time gives the value. I want to create a method that does the opposite of that. value gives time. I looked at this post: Unity get time by value but all the answers refer to inverting the curve which I think, and most comments agree, is cumbersome and error prone. I figured a better solution would be to use binary search. However, my current implementation sometimes fails to find the time.
For context the curve represents the velocity of a car as it accelerates. It is always monotonic increasing. So far this is what I've got:
float GetTimeOnCurve(AnimationCurve c, float speed)
{
if (speed < c.keys[0].value)
{
return c.keys[0].time;
}else if (speed > c.keys.Last().value)
{
return c.keys.Last().time;
}
float min = c.keys[0].time;
float max = c.keys.Last().time;
const float elipson = 0.001f;
while (min <= max)
{
float mid = (min + max) / 2;
if (speed >= c.Evaluate(mid) - elipson && speed <= c.Evaluate(mid) + elipson)
{
return mid;
}else if (speed < c.Evaluate(mid))
{
max = mid - elipson;
}
else
{
min = mid + elipson;
}
}
Debug.LogError("Speed not found");
return TopSpeed;
}
What can I do so that the time is always found?