- Home /
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;
[AddComponentMenu("Camera-Control/Mouse Look")] 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);
}
}
An invert Y toggle - sorry if my nomenclature is a little obtuse. Very common in FPS's.
Answer by Jesse Anders · Jan 08, 2011 at 09:54 PM
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.
I've updated the above with the respective code. Sorry for all the hand-holding, I'm still very much in the learning stages here.
Does that mean you still need help? (Did you try the idea I suggested?)
Thank you very much for your time already, but yes, I'm afraid I do still a bit of help! The C# code is different enough that I'm unsure how to implement your suggestion. If it were JS I think I could pull it off myself.
Thanks!
The C# version isn't going to be that different from the UnityScript version. First, you need a 'y$$anonymous$$ultiplier' variable (which you can of course call whatever you want). The syntax for declaring variables is different in C# than in US, but since you have the script right there, you can see what the syntax looks like (look at the declaration of 'rotationX' for an example). The rest of the code will be basically the same (or maybe exactly the same) in both languages. You should be able to find all the info you need in my answer above, but let me know if you have further questions.
Answer by superdupergc · Sep 15, 2013 at 06:08 PM
Easiest way to do this is to negate sensitivityY in MouseLook.cs.
sensitivityY = -15F; //for instance
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
Answer by The_r0nin · Jan 08, 2011 at 09:53 PM
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.
And yes, I noticed that you wanted to recode the mouse.cs... but why go through all of that (unless you are happy with the default system mouse icon)? This will give you much more flexibility later (depending on your game requirements)...