Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
10
Question by runevision · Nov 19, 2009 at 12:22 PM · animationrotationoptimization

Rotating objects: Script vs. Animation efficiency

Which is a more optimized way of rotating an object?

  • Rotate the object via script?
  • Animate the object and import it in to unity / animate it inside Unity with the Animation View?
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

5 Replies

· Add your reply
  • Sort: 
avatar image
18
Best Answer

Answer by runevision · Nov 19, 2009 at 12:41 PM

I tested this out and there seems to be very little difference.

I tried making a rotating cube in two ways:

With a script attached:

var tr : Transform;

function Start () { tr = transform; }

function Update () { tr.Rotate(0.0, 90.0*Time.deltaTime, 0.0); }

  • With an Animation component with an animation on it that only drives the rotation.

  • For both I tried spawning various amounts of them, with and without the MeshRenderer enabled. All objects were visible on the screen all the time.

    10 obj 100 obj 1000 obj

    Script 170 fps 140 fps 33 fps Animation 170 fps 144 fps 35 fps Script (no renderer) 174 fps 163 fps 48 fps Animation (no renderer) 175 fps 168 fps 58 fps

    As can be seen, using animation is slightly faster for high object counts. If all objects had not been visible on the screen all the time, the advantage of animations would be bigger, when the option is used to switch animations off when they're off-screen (new in Unity 2.6).

    This was tested on a Mac. I don't know if the same would apply to iPhone, but my bet would be that animations are still more efficient there. Note that this was tested inside the editor - in a real game all the fps'es would be higher, but these are good enough for the purpose of comparison.

    Comment
    Add comment · Show 6 · 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 flaminghairball · Nov 20, 2009 at 06:50 PM 0
    Share

    Interesting test. $$anonymous$$ay I ask why you use 'tr.Rotate()' rather than just using 'transform.Rotate()'?

    avatar image runevision ♦♦ · Nov 23, 2009 at 09:14 AM 5
    Share

    Like Jessy wrote in a separate answer, transform is a property that actually performs a GetTransform lookup, which is much slower than caching the Transform in a variable.

    avatar image Brian-Kehrer · Jan 23, 2010 at 06:49 AM 0
    Share

    Interesting. I'd love to see how it holds up in complex comparisons involving multiple simultaneous animations vs some well written code - maybe I'll do a test later. Still, thanks - the new animation system seems like a great addition - good to know it's even useful for simple tasks like this.

    avatar image Skjalg · Sep 20, 2011 at 09:29 AM 0
    Share

    You should have tested a script with OnBecameVisible and OnBecameInvisible as well. http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.OnBecameVisible.html

    avatar image Thaina · Dec 03, 2017 at 08:59 AM 1
    Share

    I have seen an opposite claim here

    https://www.reddit.com/r/Unity3D/comments/3stfpi/efficiency_of_spinning_100s_of_objects_using/cx09usp/

    Do we have reason why it is?

    avatar image GRASBOCK Thaina · Jun 26, 2019 at 04:34 PM 0
    Share

    This is probably because in that reddit thread he stored all of the transforms in one array so he had to only make one scriptcall per 1000 objects; while here as far as I see it, he uses 1000 Objects each having their own script, resulting in 1000 scriptcalls.

    avatar image
    16

    Answer by redthrawn · Nov 23, 2015 at 05:37 PM

    For anyone who comes across this, Unity now automatically caches the transform component for you, so you don't need to worry about caching it yourself.

    Comment
    Add comment · 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
    9

    Answer by Jessy · Nov 21, 2009 at 12:08 AM

    If you don't cache the transform, every time you use "transform", what really happens is GetComponent( typeof(Transform) ) as Transform. I always cache my transforms because of this. Here's the video where I learned about this.

    I always use new Transform transform; and that way, I can still use the word transform exactly the same, only it's faster.

    Comment
    Add comment · 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
    -1

    Answer by $$anonymous$$ · Nov 21, 2009 at 12:39 AM

    I expect the script would take very little memory, while the animation would take more than a little.

    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 runevision ♦♦ · Nov 23, 2009 at 09:26 AM 2
    Share

    I looked at the memory usage for 1000 objects in both cases and couldn't measure any difference in allocated memory between the animation solution and the scripting one. Anyway, I also created this question + answer to stop (mis)information based on pure speculation and get some hard facts on the table ins$$anonymous$$d. I think it would be nice if we could continue it in this spirit and only post answers when we've actually tested that they're true.

    avatar image swisscoder · Sep 20, 2011 at 09:24 AM 0
    Share

    test an answer to be true? interresting. depending on the circumstances and the kind of test you do, you might get results that are true only in your specific case. Testing an answer for a certain scenario, ok. But a clear true/false you not get just from a test I think. Anyway great information, and thank you for testing that stuff!

    avatar image
    0

    Answer by BenWiller1989 · Aug 03, 2020 at 06:45 PM

    That's damn clever. Well, I tested some things too. I Rotated 1000 Gameobjects with :

    1: Only a Animator.

    2: An Animator that only called an event in a script, that rotated the objects.

    3: A script in Update.

    4: A script in FixedUpdate.

    5: A Coroutine.

    The looser was 2 and the winner was 3!

    However, if you combine those components, with larger scripts, it could be different. I've created a huge AnimalAI script and ran it in Update, Coroutine, and an Animatorevent and the animator event clearly won. Both were calling 2*sec. In the end, it all depends on how you structure your code and combine your components.

    The problem with animators could be the lack of optimization possibilities in some ways.

    Comment
    Add comment · 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

    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

    6 People are following this question.

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

    Related Questions

    Simple animations - hardcoded or import animation 1 Answer

    Rotate player back to walkable position after being hit. 0 Answers

    Animations with rotations breaking ingame object rotations 1 Answer

    How do I add rotation according to mouse movement to a character even though animation is being played? 0 Answers

    Root Motion slowly veers off track 1 Answer


    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