0
\$\begingroup\$

I'm trying to rotate my characterController around with the mouse movement. I've tried to do so with this:

rotLeftRight = Input.GetAxis("Mouse X") * MOUSESENSIVITY * Time.deltaTime; rotUpDown = -Input.GetAxis("Mouse Y") * MOUSESENSIVITY * Time.deltaTime;

It works fine when I just move the mouse, but Input.getAxis returns 0 if you don't move the mouse, even if it's already on a corner of the screen. Is this normal?

What I need is something that returns 0 only when the mouse is in the center of the screen. I don't know if that is posible using Input.GetAxis or I should use another function.

Anybody can help me with this?

\$\endgroup\$
2
\$\begingroup\$

Input.GetAxis() returns the velocity of the axis in that frame. For example, getting the x-axis from a controller with a joystick will return the same thing as if you had moved your mouse to the right a certain amount. Input.GetAxis() will not tell you the overall movement of the mouse over multiple frames, or the position of the mouse on the screen.

To get the mouse position, use Input.mousePosition. If you want that value to be 0 when the mouse is at the center of the screen rather than the corner, subtract half of the screen size (this vector: new Vector2 (Screen.width/2, Screen.height/2)). For more information, check out the Unity docs: Input.mousePosition

\$\endgroup\$
  • \$\begingroup\$ Thanks, that almost solved it, the only issue now is that Input.mousePosition also detects the mouse when it's on the black borders at the side of the screen. It's a bit weird, since I already have Cursor.lockState = CursorLockMode.Confined; on the code. \$\endgroup\$ – Hector Oct 3 at 16:05
  • \$\begingroup\$ Well I managed to correct that by normalizing the values between -1 an 1, like Input.GetAxis, and if its a lower value than -1 or higher than 1 they become -1 or 1. But for some reason in 16:9 Input.mousePosition the top left corner of the screen doesn't start at (0,0,0) but at (-15, -15, 0). \$\endgroup\$ – Hector Oct 3 at 16:38
  • \$\begingroup\$ This might be happening because the game is being run in the editor. Try checking the fullscreen game view and making sure what the values for the corners are. The mouse position should be clamped between (-Screen.width/2, -Screen.height/2) and (Screen.width/2, Screen.height/2), and anything that goes beyond this will be impossible to achieve outside of the editor. \$\endgroup\$ – Koala Squad Oct 3 at 16:50
  • \$\begingroup\$ If you want the position values to be clamped between -1 and 1, divide x by Screen.width/2 and height by Screen.height/2. \$\endgroup\$ – Koala Squad Oct 3 at 16:51
  • \$\begingroup\$ It turns out that Unity by default also puts the click point of a custom cursor in the top left corner, so my crosshair click was not in the middle. I've fixed it by changing the crosshair hotspot to the center of the image I use as cursor. \$\endgroup\$ – Hector Oct 3 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.