Calculate which way you need to rotate in order to look to the left/right

I am trying to make a game where you can switch gravity to, let’s say, be to the left instead of down. When doing that, I am also changing how the player jumps, and looks around. The gravity can change depending on other surfaces around the level, which may have the rotation in them set to anything.

How can you calculate which axis to rotate? For example, when rotation is (0,0,0), you need to rotate the Y axis to look to the left and right, with the pretty standard code:

yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
transform.eulerAngles = new Vector3(pitch, yaw);

In the editor, when you are in rotation mode, no matter what rotation an object has, you can have it rotate to the left and right. How can you programmatically do that?

if I understand correctly you can use this code :

private void Update(){
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
 pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
 transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
}

A general information : Yaw Pitch and Roll is not the same as what we think. Unity Quaternions convert Quats to Euler angles in order Z , X , Y… Which means, your Pitch and Roll would not be the same when yaw changes. For example, Z / Rolll will never fail. But Pitch (Rotation around X axis) will always fail when you have different Non Zero Z and Y.

Also I am not 100% sure what you want, but if you want something to rotate On Left/Right

  1. Get the Left or right vector by using this.transform.right (and -this.transform.right for Left vector)
  2. Use Transform.Rotate, if you want to rotate the transform.
  3. Use Quaternions to rotate generally, its much easier.