- Home /
Quaternion.AngleAxis setting other axis to Zero
I have an object with the x angle rotated about 55 degree facing up. I want that x angle to keep at 55 but the Quaternion.AngleAxis keeps resetting it to 0 while rotating the object on its Z axis. how would I change the code to fit this so the object can have a static x rotation of 55 while the object can rotate on its z axis?
Here is a video: http://www.screencast.com/t/NWYwODYyZjE
pay attention to where the z axis is pointing at the beginning then notice how it zeros out when it starts to rotate.
Answer by runevision · Mar 29, 2010 at 08:37 AM
Quaternion.AngleAxis doesn't modify an existing rotation, it creates a new rotation.
If you want to rotate by the rotation created by Quaternion.AngleAxis, you can do it by multiplying the new rotation on the left side of the existing one:
myRotation = Quaternion.AngleAxis(myAngle, myAxis) * myRotation;
this is what happened when i used this code: http://www.screencast.com/t/OTE0NWE4$$anonymous$$DAt It just continually spins with no control to the direction I am touching the screen.
I started out with my camera angles being 0. It looked like this: http://www.screencast.com/t/YmE3$$anonymous$$zQz$$anonymous$$Tgt. what you are seeing here is the arrow pointing to where i am touching.
but now I decided that i was going to angle the the camera down, so the gameobject with the arrow has to be facing the camera so thats when I moved its angle to 55. The video up top shows where i am trying to get at.
myRotation is just an example. Replace it with whatever it is you want to rotate. If you don't want the rotation to rotate further each frame, but rather be calculated from scratch each frame, you need to reset it before applying the rotations.
@Rune please correct me if I'm wrong, but it seems like in the forums the actual solution was almost the same, except the new rotation was multiplied on the right side and on top of an initial value rather than on the existing one. I still can't grasp quaternions...
Answer by msknapp · Sep 23, 2011 at 03:33 PM
I highly recommend that you use the Transform functions "Rotate" and "RotateAround" instead of modifying the Quaternion directly. You will save yourself a lot of headaches.
// when instantiating or starting your game object, start with
myGameObject.transform.Rotate(Vector3.forward,55); // may need to use -55 instead.
// then during runtime, only allow rotations such as:
float degreesAngle = 5;// or any number you choose.
myGameObject.transform.Rotate(Vector3.up,degreesAngle);