I am attempting to control an object’s Roll/Pitch/Yaw via GUI controls. The GUI controls just out put an angle between -180 and 180 degrees. I have a GUI_Controller script that collects the rotations from each of the 3 GUI components and then rotates the selected GameObject.
I have come up with two ways to do this, but each has a drawback that I have been unable to think of a solution for.
First, some variable definitions:
rollControl.RotationAngle is the Roll amount in degrees of type float
pitchControl.RotationAngle is the Pitch amount in degrees of type float
yawControl.RotationAngle is the Yaw amount in degrees of type float
SelectedShip is the GameObject to be rotated of type GameObject
rollAngle, pitchAngle, yawAngle all hold their respective angles between Update cycles so I only call the rotation logic as needed.
The following two code segments are in my void Update() function (only one uncommented at a time)
//This code's issue: Cannot adjust Roll if Pitch or Yaw have an angel. Cannot adjust Pitch if Yaw has an angle.
if (rollControl.RotationAngle != rollAngle || pitchControl.RotationAngle != pitchAngle || yawControl.RotationAngle != yawAngle)
{
SelectedShip.transform.rotation = Quaternion.identity;
SelectedShip.transform.RotateAround(Vector3.zero, SelectedShip.transform.forward, -rollControl.RotationAngle);
rollAngle = rollControl.RotationAngle;
SelectedShip.transform.RotateAround(Vector3.zero, SelectedShip.transform.right, -pitchControl.RotationAngle);
pitchAngle = pitchControl.RotationAngle;
SelectedShip.transform.RotateAround(Vector3.zero, SelectedShip.transform.up, yawControl.RotationAngle);
yawAngle = yawControl.RotationAngle;
}
//This code's issue: Cannot return to zero rotation properply. Must return to zero by undoing the rotations in the order they were done
if (rollControl.RotationAngle != rollAngle)
{
SelectedShip.transform.RotateAround(Vector3.zero, SelectedShip.transform.forward, -rollControl.RotationAngle + rollAngle);
rollAngle = rollControl.RotationAngle;
}
if(pitchControl.RotationAngle != pitchAngle)
{
SelectedShip.transform.RotateAround(Vector3.zero, SelectedShip.transform.right, -pitchControl.RotationAngle + pitchAngle);
pitchAngle = pitchControl.RotationAngle;
}
if(yawControl.RotationAngle != yawAngle)
{
SelectedShip.transform.RotateAround(Vector3.zero, SelectedShip.transform.up, yawControl.RotationAngle - yawAngle);
yawAngle = yawControl.RotationAngle;
}
I would have thought that the first segment would solve my issue. I’m reapplying the rotation values in Unity’s Z-X-Y order every cycle. So even if I am only changing the roll amount the roll/pitch/yaw should still be getting reapplied to the temporary GameObject, which starts with no rotation. Yet, after I adjust the object’s Yaw the roll is being applied to the World Z axis instead of the Object Z axis.
I can supply the project file if needed.
Edit: Removed the temp object from the first code segment. Functionality did not change but was a performance improvement recommended by Polymorphik.