Animation Curve or Coroutine? Performance question.

Ok, so let’s say that you want to, for example, modify an object’s scale, or change the color of a material of an object: what is best to use? A coroutine or an animation curve?

If you set up a coroutine, you would need to make some calculations yourself and then pas the result in a Lerp function (for example). Then yield for 1 frame, and repeat until you finish whatever you need to do.

On the other hand, setting an animation curve “does not” have any calculations, but doesn’t Unity still need to interpolate to find values in between the different points of the animation curve?

So, which one is the best to use?

Coroutine and AnimationCurve are two totally different things. You could say, have an AnimationCurve in a coroutine. Also AnimationCurve does have calculations, that’s what the Evaluate() function does.

As for performance, both are minimal. The performance of the AnimationCurve depends on the number of keys it contains. Compared to a simple Mathf.Lerp calculation, the Lerp will be faster. But that’s because Mathf.Lerp is a simple linear interpolation, where as AnimationCurve can be much more complex.

As for which is best to use, that depends on your goals. Do you plan on using non-linear interpolation and wish to set up your own values? Then AnimationCurve would be more useful. If you only care about linear interpolation, then Lerp will do the job just fine. You could also have the AnimationCurve evaluate a linear curve. AnimationCurve is basically a lerp.

Does it matter if you use a coroutine or update? Not really, use what you’re comfortable with.