I have an object. The object's initial rotation y axis is set to -90. I want to change the rotation to 90. I do it by following. However it changes to -90 degrees. Why does this happen and how do I make it positive at all times? X=180, Y= -90, Z=180
void Start()
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, -90 180, transform.eulerAngles.z);
}
CodePudding user response:
When using the .eulerAngles property to set a rotation, it is important to understand that although you are providing X, Y, and Z rotation values to describe your rotation, those values are not stored in the rotation. Instead, the X, Y & Z values are converted to the Quaternion's internal format.
Since several euler-angles describe the same rotation, there is no guarantee that you will get the same euler angles as you assigned. If you need this you may need to normalize the angles yourself.
My preference is to avoid euler angles as much as possible. In many cases you can use a vector and a up-direction to create a quaternion with the LookRotation. I find this easier since I have a much easier time to intuitively understand a direction-vector than an euler angle. Quaternions themselves are really hard to understand, but I find them fairly straightforward to use even if you do not understand the math behind them.
CodePudding user response:
While the angle is less than 0°, add 360°. Then while the angle is greater than or equal to 360°, subtract 360°.
If you are sure that the angle does not pass the range [-360°, 720°), you can trade the while's for if's.