- Home /
Cursor not locking to center
I don't know why this is happening and I can't figure it out.
I'm using Cursor.lockState = CursorLockMode.Locked;
and Cursor.visible = false;
to lock it, and Cursor.lockState = CursorLockMode.None;
+ Cursor.visible = false
to unlock it when I open a menu. It seems that setting the visibility is redundant but I wanted to be sure.
This is how I want it to behave, in game you get a crosshair, in menu you get the mouse. The problem lies when I pull up the menu and call it to lock again - it does lock, but it locks in the last position. I'm sure there's a not-so-problematic way to center the mouse on the screen but the documentation explicitly states that Cursor.lockState = CursorLockMode.Locked;
"Lock[s] cursor to the center of the game window."
Is this a bug?
And for the sake of it, how would you move the mouse to the center? If I could get it there the lock would be fine. Is there a method for it? Would you have to get the screen width and height in pixels and divide by 2 like with some old OnGUI stuff, or is there a better way?
This seems to be bugged in Unity 5.5
in prior versions it was my experience that it did lock to centre of the screen and would still give you pointer events however in 5.5. I have noticed that it seems to lock to lower left of the screen or elsewhere. I have also noticed that when locked and hidden its not sending events save for click event ... I am not sure if this is a bug or intended ... I hope its a bug as it would make zero since why they would remove an obviously critical feature for many game types e.g. use centre of screen as cursor position hiding the pointer its self which previously was as simple as hide cursor, lock cursor.
As to moving the cursor sadly Unity doesn't expose this that I am aware of, there are a number of assets that allow you to do this though. I would love to hear from Unity on how we lock the cursor to the centre of the screen and how we get pointer events to keep working with a hidden cursor ... I really hope this a simple bug and not intended functionality.
Your answer
Follow this Question
Related Questions
Hiding a Cursor? 3 Answers
Cursor Lockmode only works once? 1 Answer
Cursor won't stay locked in update!! 0 Answers
Locking cursor/mouse on an object 1 Answer