- Home /
Setting rotation of child GameObject
Hi there
Here's the deal: There's an item I want to parent to an anchor. The item's desired rotation values were adjusted manually in the editor runtime and entered in the item's start function for future reference. The program parents the item to the anchor and sets its position, rotation and scaling by these values. Position and scaling work all right, but not the rotation. How am I supposed to set the rotation of this child object via script? I tried rotation, localRotation, eulerangles, everything in every possible combination, but I couldn't get he exact same values I set manually in the editor? Any solution, guys? Thanks in advance.
transform.rotation should work just fine (just as transform.postion and scale worked fine) but there is a difference between the object local rotation and global rotation. Since the object is a child you need to change its transform.localRotation. Here is more info: http://docs.unity3d.com/Documentation/ScriptReference/Transform-localRotation.html
I actually tried that too like for half a day, to no avail. This is how it is at the moment (not working):
Quaternion rot = t.gameObject.transform.localRotation;
rot.x = itemScript.inventoryRot.x;
rot.y = itemScript.inventoryRot.y;
rot.z = itemScript.inventoryRot.z;
t.gameObject.transform.localRotation = rot;
t.gameObject is parented to said anchor... inventoryRot is set at spawning the item in Start().
Answer by MakeCodeNow · Mar 05, 2014 at 05:59 PM
The x/y/z components of a Quaternion are not Euler angles, so you can't just set them like you are in the comment. Assuming that itemScript.inventoryRot is a copy of the rotation values you saw in the editor, you probably want something like:
t.gameObject.transform.rotation = Quaternion.Euler(itemScript.inventoryRot.x, itemScript.inventoryRot.y, itemScript.inventoryRot.z);
Note that you may need to use .localRotation =, depending on what you are doing.
Yes, that was the trick. With localRotation... thank you very much, mate. :)
Your answer
Follow this Question
Related Questions
Maintain world rotation on parenting to another object 0 Answers
Code to rotate around a local axis until local Y is 0? 0 Answers
Local child rotation and transform.LookAt 2 Answers
Complicated Rotation Issue 1 Answer
Child versus Parent rotations 3 Answers