vote up 3 vote down
star
1

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?
flag

3 Answers

vote up 5 vote down
check

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.

link|flag
Interesting test. May I ask why you use 'tr.Rotate()' rather than just using 'transform.Rotate()'? – FlamingHairball Nov 20 at 18:50
3 
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. – Rune Skovbo Johansen Nov 23 at 9:14
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. – Brian Kehrer Jan 23 at 6:49
vote up 4 vote down

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.

link|flag
vote up -1 vote down

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

link|flag
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 instead. 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. – Rune Skovbo Johansen Nov 23 at 9:26

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.