- Home /
Rotation Performance
Can anyone see why this might be happening?
I am seeing some really terrifying profiler output on a very simple call: transform.rotation_set. Setting the rotation of an actor seems to be taking very long. There are a large number of actors in the scene, but each of them are performing operations which seem much more complex than just setting rotation. Rotation is taking up 1/4 of the AI calculation, even when run next to a lot of physics and pathfinding code.
Each actor has between 20-30 children, and most of them are just empty nodes part of the bone hierarchy. Each actor has a skinned mesh renderer, a kinematic rigidbody, an animation, and a capsule collider. Could any of these components the culprit to this performance impact?
Any insights on how to improve this would be great. I'm trying to optimize these actors as much as possible.
It's a bit late, but same for me! $$anonymous$$y object also has a complex animated hierarchy, and a CharacterController component... And I'm setting the rotation in FixedUpdate().
Answer by adbourdages · Oct 17, 2012 at 03:15 AM
I have just discovered the function RigidBody.MoveRotation() didn't have the same impact as setting the rotation of the rigidbody's tansform. You pass a target rotation and internally uses forces to get the rigidbody rotation to the proper value.
Just wanted to thank you for posting about this magical function. We have complex objects (20+ children each) that used transform.rotation = ..., and switching to RigidBody.$$anonymous$$oveRotation() had a significant impact on our framerate.
As for our theory on why setting transform.rotation is so slow: It might be calling GetComponent on every subobject as it updates their transforms. That's the only reasonable explanation we can imagine for why it's so horrendously slow and inefficient compared to RigidBody.$$anonymous$$oveRotation().
I know this is very old, but the aside from the possible GetComponent<Transform>()
(which I think was optimised in Unity 5) the actual reason why modifying the rotation (or position) of a game object with rigidbody is slow, is that this circumvents the physics system and all the physics of the object needs to be recalculated. I've found a comparison of the performance of setting position different ways, and the result was that if you have a rigidbody, ALWAYS use Rigidbody.$$anonymous$$ovePosition()
and $$anonymous$$oveRotation()
.
Answer by JanWosnitza · Oct 16, 2012 at 04:23 PM
You could try to set the rotation in FixedUpdate. Since all physics is singlethreaded (an other thread than Update) it may be that setting the rotation will wait until next FixedUpdate.