Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MSpiteri · May 27, 2016 at 11:20 AM · animationcoroutineperformance optimizationcoroutinesefficiency

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MSpiteri · May 29, 2016 at 03:49 AM 0
Share

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?

avatar image Jessespike MSpiteri · May 31, 2016 at 07:00 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

function behaves unexpectedly inside a coroutine. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges