- Home /
Making world rotation of a child object fixed efficiently...
I have an object which is parented under another object. And I want the childs world rotation to be fixed at (0,0,0). I know I can do:
Update() {
transform.rotation = Quaternion.identity;
}
But that would be really inefficient. Is there a way to disable rotation inherited from the parent from Awake() or Start(). That way I only have to do it once, instead of every frame.
PS. I dont want to attach a rigidbody and put on constraints. A solution in code would be better.
Thanks!
I think there is no other solution then you are actually doing or just to break the parenting and follow the transform.position only of the parent so far on Update.
there isnt any way to constraint rotation without a rigidbody?
Answer by Eric5h5 · Jan 02, 2012 at 07:22 AM
The definition of a child is that it inherits position and rotation from the parent, so if you don't want to reset rotation every frame, then it shouldn't be a child in the first place. There's nothing magical about rigidbody constraints, though; I expect they do pretty much what your code is doing.
I don't think adding a RigidBody works — AFAICT freezing rotation on a rigidbody freezes it relative to the parent.
Answer by IMD · Apr 15, 2014 at 01:43 PM
When you want to leave the gameobject as a child for 99 other reasons, you can true rotate it independently of the parent by adjusting the child's local rotation by its parents rotation and then apply the true desired 'global rotation' to the child. Here's some code I wrote to do do this:
transform.localRotation = Quaternion.Euler(-transform.parent.rotation.eulerAngles + trueRotation);
Hope you find it helpful.
maybe I'm dumb but trueRotation here is the rotation we are converting to the new rotation? I'll try it out and see if it works.