I have the following code:
if (Input.GetKeyDown(KeyCode.A))
{
var rot = switchTransform.localEulerAngles;
Debug.Log(rot);
rot.x = 150;
switchTransform.localEulerAngles = rot;
}
When I click 'A', the switchTransform
switches from (30, 0, 0) to (150, 0, 0) in the inspector and (30, 0, 0) to (30, 180, 180) in the console.
It works correctly if I write it like this:
angle = 150f;
if (Mathf.Abs(angle) <= 180 && Mathf.Abs(angle) > float.Epsilon)
{
angle = 180 - angle;
}
if (Input.GetKeyDown(KeyCode.A))
{
var rot = switchTransform.localEulerAngles;
rot.x = angle;
switchTransform.localEulerAngles = rot;
}
Why does it happen, and is there a better way to work around it?
This is the switch transform, as you can see it has unusual pivot.