- Home /
What causes a rotation to reset after instantiation?
I have encountered a very unusual issue and hope somebody can explain it to me and help me find a solution. Basically, here is what I'm trying to accomplish: I want to instantiate a game object, set it's parent, tweak it's position, and make sure there is no rotation. My code:
Transform player = GameObject.FindGameObjectWithTag(Tags.player).transform;
//instantiate the object
GameObject itemInHand = (GameObject)Instantiate(ItemDictionary.blah blah balh);
//set the parent
itemInHand.transform.SetParent(player);
//tweak it's position
itemInHand.transform.localPosition = new Vector3(-0.681f, -0.547f, 1.57f);
//make sure there is no rotation
itemInHand.transform.rotation = Quaternion.identity;
Everything works great except for the rotation. When I step through the code, it works and is set to zero, but as soon as I step out of the code and the game resumes, the rotation is set to a random value. Anyone know why that would happen?
This might be related to global vs. local rotation if itemInHand's parent is rotated such that its coordinate axes are not aligned with those of the global/world frame. Your code is currently setting itemInHand's rotation such that it isn't rotated relative to the global coordinate frame; perhaps you should be setting its tranform's localRotation property ins$$anonymous$$d.
Also note that Instantiate() sets the new object's global rotation to Quaternion.identity by default, so the last line of your current code will have no effect.
Answer by wmcritter · Mar 27, 2015 at 03:29 PM
yeah, all I had to do was change .rotation to .localRotation.