Fixing Inverted Camera Controls When Flipped, MouseOrbit

Hello,
thank you very much for any help you can provide.

I’m using a script called MouseLook. It’s very similar to MouseOrbit, that comes with Standard Assets. I attach MouseLook to my camera and set the Ymin to -360 and the Ymax to 360. My target object is a sphere. This now enables me to rotate infinitely in any direction around the sphere. All good so far.

The problem I’m having is when the camera flips upside down, my ‘Left/Right’ controls become inverted. So instead of moving mouse left to move camera left, moving mouse left moves the camera right. I want to fix the controls so that they are always consistent visually.

My guess is the logic should be something along the lines of, “If camera is upside down: invert Left/Right controls”, and that should cancel out the effect this is having. Unfortunately my scripting/math skills are very beginner.

Here’s where I’m at:

zRotation = GameObject.Find("Main Camera").transform.rotation.eulerAngles.z;
		
		if (axes == RotationAxes.MouseXAndY)
		{
			if (zRotation == 180){
				
			// Read the mouse input axis
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

			rotationX = ClampAngle (rotationX, minimumX, maximumX);
			rotationY = ClampAngle (rotationY, minimumY, maximumY);
			
			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
			
			transform.localRotation = originalRotation * xQuaternion * yQuaternion;
			}
			else{
			
			Debug.Log (zRotation);
			
			
			// Read the mouse input axis
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

			rotationX = ClampAngle (rotationX, minimumX, maximumX);
			rotationY = ClampAngle (rotationY, minimumY, maximumY);
			
			Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
			Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
			
			transform.localRotation = originalRotation * xQuaternion * yQuaternion;
			
			}

if “zRotation” == 180, than I know my camera is flipped. But I’m having a hard time understanding the code at that point. What’s a good way to flip the controls?

2 Answers

2

if (zRotation == 180)

this condition only works when the angle is EXACTLY 180. you will want to change this to something like

 if (zRotation >= 180)

for all angles including and exceeding 180.
you can then use something like

 rotationX += -Input.GetAxis("Mouse X") * sensitivityX;
 rotationY += -Input.GetAxis("Mouse Y") * sensitivityY;

to invert the input values.

Thank you Bubsy, but it seems that this is now preventing me from rotating to a certain degree. The reason i chose to use zRotation == 180, is because for some reason my zRotation is always < 180 when the camera is right side up, and always == 180 when it is upside down. For some reason It never is > than 180. Maybe that's a clue! Anyways, thank you again for your help. I really hope we can figure this out.

–

Did you seriously get that to work? Don’t these values need to scale?