- Home /
Properly Rotating Child Objects by Script
I have been having a problem with trying to rotate a child object smoothly but it will switch between inheriting the rotation of the parent, and then to actually paying attention to the script on what its rotation should be. The line responsible for this rotation is this:
transform.eulerAngles = new Vector3(xRot, yRot, 0);
Is this the way to do this, or is there a better way? Thanks!
Children inherit their rotation from their parent. If you want to rotate a child independently then you would need to use
transform.parent.rotation
to get the parent's rotation and then use that to calculate the child's rotation.
aww that actually makes a lot of sense @RoguelikeShadow. Thanks!
and now it is broken again.... just as soon as I opened the editor. This is what I have been dealing with for the past week, I find a solution and then it just breaks out of the blue for now reason.
Answer by RLin · May 16, 2015 at 10:24 PM
Actually, it is much easier to use transform.localeulerangles. This is the child's rotation relative to the parent's.
Through experimentation I have found that using localeulerangles does some weird things with rotations, when I just use the xRot and yRot varialbes. I don't know why but it just does. But in order to make that work you use localeulerangles anyways after I run through those other steps.