- Home /
Using Math to adjust a transforms angle about an axis?
I'm currently using Math to adjust a gameobjects angle about a specific axis (Euler angles aren't sufficient because of the fact that one quaternion relates to several Euler angles).
In the below code I want to get my transforms current rotation (in degrees) about its z axis and to adjust it such that its new rotation is 0 degrees about its z axis (In Euler angles it would be localEulerAngles.z = 0). The problem is that sometimes, unexplicably, it does not perform as I'd expect. Surely if I get the rotation about the z axis in degrees, rotate around the z axis negative those degrees and do another test to get the current rotation about z then it should be 0?
Heres a sample of my code that I'd expect to do that:
Vector3 currentUp = transform.up;
float currentAngle = (Mathf.Atan2(currentUp.x, currentUp.z) * Mathf.Rad2Deg) + 180.0f;
transform.RotateAround(transform.position, transform.forward, -currentAngle);
Vector3 adjustedUp = transform.up;
float adjustedAngle = (Mathf.Atan2(adjustedUp.x, adjustedUp.z) * Mathf.Rad2Deg) + 180.0f;
//I would expect the adjusted angle to always be "0" at this point but it's often not.
//Surely we just got the angle about the local z axis (currentAngle) and then rotated the object back "currentAngle" degrees?
Any help would be much appreciated, I'm truly stumped!
Thanks guys :)