- Home /
Rotating a certain axis offsets the other ones?
Hello. Everytime I try to rotate my object using my code, it offsets the x rotation to 0 again. I think it has something to do with me defining my rotation when I instantiate the object, but I'm not sure. Here is my code:
if (!ghostOn)
{
ghost = (GameObject)Instantiate(Building[SelectedBuilding],
new Vector3(hit[i].point.x - hit[i].point.x/2,
hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2),
hit[i].point.z - hit[i].point.z/2),
Quaternion.Euler (-90, 0, 0));
ghostOn = true;
}
if (ghostOn && Input.GetKeyDown (KeyCode.R)) {
ghost.transform.rotation = Quaternion.Euler(ghost.transform.rotation.x, ghost.transform.rotation.y + 90, ghost.transform.rotation.z);
}
Answer by robertbu · Aug 24, 2014 at 10:22 PM
You have a serious problem here, and a potential problem here. The serious issue is your use of ghost.transform.rotation. 'transform.rotation' is a Quaternion, a non-intuitive 4D structure in which the individual x, y, z, and w components have values between -1 and 1. Since you are looking for angles, you may be able to use transform.eulerAngles, but I highly recommend treating eulerAngles as write-only. Plan you code so you never have to read from them.
So to fix your code, I would replace line 13 with:
ghost.transform.Rotate(0,90,0);
...or
ghost.transform.Rotate(0,90,0,Space.World);
...depending on what you are looking for.
Your answer
Follow this Question
Related Questions
Why is this rotation acting odd? 0 Answers
Quaternions acting up 1 Answer
Change rotation of child gameobject 1 Answer