- Home /
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?
Answer by Jessespike · May 27, 2016 at 04:58 PM
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.
Hello and thanks for the reply. When I said "Animation Curve" I was not referring to a variable. I was referring to an animation that you then trigger with an animator controller. Let me explain a bit more with an example.
Let's say that I want to make an object appear on screen by changing its scale smoothly from (0,0,0) to (1,1,1). I could do it in 2 ways:
(1) Either use a coroutine to Lerp the localScale of the transform from Vector3.zero to Vector3.one over a time of 1 seconds for example.
or
(2) Create an animation (from the menu "Window > Animation") that takes the x, y, z scale values of the transform from 0,0,0 to 1,1,1 in a straight line (similar to a Lerp). Then I assign this animation to an Animator component in my gameObject and trigger the animation when I want the gameObject to appear on screen.
Which of the above would be more efficient. Let's say you are making a mobile game, where battery life is important. Which one is less expensive for the processor?
Also, another question on Animation curves (from the animation window) ... I think that an Animation Curve is expensive depending on the shape because I assume that Unity creates an equation y=f(x) such that it passes through all the keys and at the particular gradient. So this could have x^2, x^5, etc, depending on the complexity of the curve. Am I right?
Animations use AnimationCurves. Sometimes alot of them, this is how animations animate. They're not that expensive. A simple lerp is cheaper though.
Unless you have thousands of keys or something. The difference probably won't be noticeable. So I doubt battery life will be affected at all. I don't really know the math theory behind Animations or Curves, but I'm pretty sure the amount of keys does affect the computation. Either case, I feel this is a micro-optimization that is getting more attention than it needs.
If you want to be extremely frugal, then use a lerp. There is still nothing wrong with using an animation to accomplish your goal though.
Your answer
Follow this Question
Related Questions
My Coroutine does not get called 4 Answers
Use Coroutine when object rotates 2 Answers
Wait for coroutine to finish before returning a value in another function 0 Answers
How to make functions async? 2 Answers