I have this code:
Debug.Log("MouseHalo Transform.Rotation vector x: " + target.rotation.x + " y: " + target.rotation.y);
var rotation = Quaternion.Euler(x, y, 0);
transform.rotation = rotation; // Rem this out to freeze
Debug.Log("MouseHalo after setting Rotation vector x: " + transform.rotation.x + " y: " + transform.rotation.y);
I am trying to set a gameobject’s z axis in a certain direction but need to read what the x and y are currently?
The object’s x axis stays 0.5, but the Y axis flips to 180.5.
Thanks.
transform.rotation will give you a Quaternion representation of the rotation, so the x and y parameters don’t directly correspond to the Rotation x and y fields of the Inspector. Very rarely does it make sense to read or manipulate a Quaternion’s x/y/z/w parameters directly.
To get the Euler angles shown in the inspector, use Quaternion.eulerAngles:
Vector3 eulerAngles = transform.rotation.eulerAngles;
Debug.Log("transform.rotation angles x: " + eulerAngles.x + " y: " + eulerAngles.y + " z: " + eulerAngles.z);
For the second part of your question, if you want to point the GameObject’s z+ axis in a particular direction, the easiest way is:
transform.rotation = Quaternion.LookRotation(zDirection);
You can optionally include a second parameter for the direction its y+ axis should try to point toward:
transform.rotation = Quaternion.LookRotation(zDirection, yDirection);
Note that if zDirection and yDirection aren’t perpendicular, the zDirection takes precedence - the resulting rotation will put the y+ axis as close as possible to yDirection, but not perfectly aligned. There are some tricks you can use to invert this behaviour, if you want the yDirection respected exactly and the zDirection as close as possible.
hey can i convert back eulers angle to quaternion?