# Toggle the Y axis Invert in MouseLook.cs with a keystroke?

Howdy,

I'm a javascript guy, and I can't seem to figure out how to implement toggle to flip the Y axis sensitivity (aka invert mouse) in MouseLook.cs. I would like for the user to be able to hit the "I" key and toggle the Y-axis.

Thanks!

Here's the MouseLook.cs code:

using UnityEngine;
using System.Collections;

public class MouseLook : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;

public float minimumX = -360F;
public float maximumX = 360F;

public float minimumY = -20F;
public float maximumY = 20F;

float rotationX = 0F;
float rotationY = 0F;

Quaternion originalRotation;

void Update ()
{

if (axes == RotationAxes.MouseXAndY)
{
// 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 if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);

Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);

Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}

void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}

public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}

Does this:

Input.GetAxis("Mouse Y")

Appear somewhere in the script?

If so, I imagine all you'd need to do is negate that value when the 'invert' option is activated.

There's a few different ways you could implement this. One would be to store a multiplier as a member variable, initialized to 1 or -1 depending on whether the 'invert' option is on initially. You can then use Input.GetKeyDown() or GetButtonDown() to detect when the 'invert mouse y' button is detected, and negate the multiplier. (This can of course also be done in response a GUI event, etc.).

Then, simply multiply the return value of Input.GetAxis("Mouse Y") by the multiplier.

Easiest way to do this is to negate sensitivityY in MouseLook.cs.

sensitivityY = -15F; //for instance
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

Well, if the input manager won't let you select that at runtime (I honestly don't know), then you just create a custom cursor for your gameplay and an "invertMouse" boolean variable:

if (!invertMouse){
cursorPos.y = Screen.height - mousePosition;
}
else{
cursorPos.y = mousePosition;
}

I think that the mouse Y position is already inverted with respect to the GUI coordinate system, which is why it looks backwards in the code.