Artificial Mind
Artificial Mind

Reputation: 975

Unity's Input.mousePosition is off by 20px in y direction

In my player controller's update, I'm doing:

Cursor.lockState = CursorLockMode.Locked;
Debug.Log(Input.mousePosition);

I've set the game to 1920x1080 (Debug.Log(Screen.width + "x" + Screen.height) confirms that).

The resulting log writes (960.0, 560.0) (for the mouse position) while I strongly expect (960.0, 540.0) (off by 20px in y direction).

Unity version is 2017.3.0f3 Personal, OS is Windows 10 x64 if that's relevant.

Is this expected behavior that I somehow missed? Is this a genuine bug that I should report (where?)?

Upvotes: 2

Views: 1942

Answers (2)

Basile Perrenoud
Basile Perrenoud

Reputation: 4112

According to the doc of CursorLockMode, the lock option will

Lock cursor to the center of the game window

Note that it says game window, not screen. Do you per chance have a 40 pixel menu bar on top of the window? I think that your mouse is in the center of the window, that's is. So that would be to expected behaviour

Upvotes: 1

Doh09
Doh09

Reputation: 2385

Possible reasons that I see:

  • Since you use the Input.MousePosition, perhaps the mouse is placed wrongly somehow? How do you place the mouse position to be at the middle of the screen? It would be good if you displayed this code.

  • The mouse cursors hotspot offsets the result 20 pixels, as it is not set correctly according to the mouse set and used. Here is a description of what the hotspot does: hotspot The offset from the top left of the texture to use as the target point (must be within the bounds of the cursor).

Source concerning hotspot: https://docs.unity3d.com/ScriptReference/Cursor.SetCursor.html

Possible solutions that i see:

  • Adjust how you place the mouse location.

  • Add a buffer zone variable so you always deduct -20 from the mouse position.

  • Adjust the hotspot.

You don't use any raycasts do you? If yes, please show that code.

Upvotes: 0

Related Questions