Inverted FPS mouse

I was watching a tutorial on how to make an FPS controller. According to the tutorial, I had to use this code for my mouse look code

        float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * mouseSensitivity;
        float mouseY = -Input.GetAxis("Mouse Y") * Time.deltaTime * mouseSensitivity;

        rotY += mouseX;

        rotX -= mouseY;
        rotX = Mathf.Clamp(rotX, -90, 90);

        //rotate cam and orientation
        transform.rotation = Quaternion.Euler(rotX, rotY, 0);
        orientation.rotation = Quaternion.Euler(0, rotY, 0);

Despite it working perfectly in the tutorial, when I playtested my controller, the mouse was inverted. I fixed it by changing it to:

     float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * mouseSensitivity;
        float mouseY = -Input.GetAxis("Mouse Y") * Time.deltaTime * mouseSensitivity;

        rotY += mouseX;

        rotX += mouseY;
        rotX = Mathf.Clamp(rotX, -90, 90);

        //rotate cam and orientation
        transform.rotation = Quaternion.Euler(rotX, rotY, 0);
        orientation.rotation = Quaternion.Euler(0, rotY, 0);

I’m not sure why the tutorial tells me to add the input values to my rotation floats and I’m afraid that my edit to the code may come back to bite me later.

Can someone please explain why the mouse is inverted or why I need to add the input to my rotation floats. Thx!

pretty sure a negative in front of any numerical variable inverts it. So just get rid of that minus symbol in front of that

3 Likes

Oh I didn’t see that! Dang I’m blind. Thanks a bunch

1 Like

That’s exactly why writing code lines like this:

… is bad.

Do it step by step, one thing at a time.

  • get the value into a float
  • scale it by mouseSensitivity
  • scale it by Time.deltaTime
  • use it

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

You also are NOT writing faster code by putting it on one line. That’s not how code works.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums