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?

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.

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.

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.

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.

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