- Home /
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?
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.
Interesting test. $$anonymous$$ay I ask why you use 'tr.Rotate()' rather than just using 'transform.Rotate()'?
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.
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.
You should have tested a script with OnBecameVisible and OnBecameInvisible as well. http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.OnBecameVisible.html
I have seen an opposite claim here
Do we have reason why it is?
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.
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.
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.
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.
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.
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!
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.