Rotating Objects - Script vs Animation

Which is a more optimized way of rotating an object?

Rotate the object via script or animate it and import it in to unity?

I would like to know this also.
So far I have figured that scripting it might be the best way, but that is just guessing. :stuck_out_tongue:

If you animate it, you’re moving the vertices, not the object. It will look like it is facing a different direction, but the object’s axes will not have moved.

Ahh yes. That could be useful.

But which is the more efficient in terms of performance just on that single act (ignoring anything else)?

I don’t know for sure but my best guess would be that the cost for either must be almost nil as long as there’s a 3d card. Animation would be reading instructions from a file I think, while rotating in the game engine is some math which 3d cards are created specifically to do very fast.

If you’re going to do something simple like rotate an object 90 degrees or if you want a continuous movement like a planet rotating, using scripting to rotate the object is as easy as it gets and is probably much faster and less expensive.

I suppose animation will be slower.

This is my recollection of writing shaders in HLSL, so I don’t know how well this equates to Unity’s way of doing things, but 3D stuff is all matrix mathematics, when you get down to it.

The unanimated mesh will have its vertices manipulated by a matrix which represents the camera’s fov etc. and a matrix which represents the mesh’s Transform, which includes translate, rotate, and scale.

The animated version will go through exactly the same, but it will then also be manipulated by a matrix which represents the animation.

So if the mesh’s Transform is used in both, you might as well set the rotation in its Transform.

But as woundedkarma says, the difference is miniscule.

There is a bit of misinformation in this thread.

I tested it out, and using animation is slightly faster, but the difference is pretty small unless you have a very high amount of objects.

See the full answer here:
http://answers.unity3d.com/questions/481/rotating-objects-script-vs-animation-efficiency/484#484

Rune

Thanks for all the input guys!

I didn’t even think to look in Unity Answers for something like this…next time I will check there also. I learned 2 things from this post! :smile:

Cheers!

UnityAnswers is for all questions that are not argumentative or require extended discussion.

I made the question and answer there just for you. I posted the answer there instead of here because it will be easier for other poeple to find it there in the future. Feel free to vote them up if you found the question good or the answer helpful.

We are trying to get people to use UnityAnswers more, because it’s way easier there to find questions and answers that have already been covered in the past.

Rune

Out of curiosity, what are the results if you optimize the script so that int->float conversions are taken out? i.e., this:

tr.Rotate(0, 90*Time.deltaTime, 0);

should ideally be this:

tr.Rotate(0.0, 90.0*Time.deltaTime, 0.0);

–Eric

I just tried that and it didn’t result in any measurable difference (not even 1 fps difference at 1000 obj).