- Home /
Centre cursor - Unity 5 (not locking or making invisible)
The legacy way of locking the cursor would set the cursor locked at the centre of the screen:
Screen.lockCursor = true;
The new way of locking the cursor is fine apart from the cursor is locked in position wherever it was at the start of the game:
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
How do I move the cursor to the centre of the screen so that players can still click on buttons etc?
This is probably a duplicate question (from another one I can't find right now).
If you set the cursor to the center of the screen, why do you need the cursor position to do anything? If it's a 3D button just send a raycast through the center of the screen (check example here), if it's 2D then check if your sprite rect contains the center point of the screen.
Of course I could do a raycast, but Unity 4.6+ with the new GUI system handles hover and click detection with the cursor on buttons for you, so why do the extra work?
Found the question (it was a thread in the forum) here.
If it's GUI then I don't know why you would lock the cursor (how would you click on a topright corner button?).
Answer by Andres-Fernandez · Jul 21, 2015 at 06:57 AM
The first 3 entries I got by googling "unity set cursor position" where quite useful. Specially this one.
There's also an asset that apparently does what you need (I havent tried it).
I contacted the asset creator and it sounds like it's essentially the same technique (import the windows dll). Thanks, it works. Will have to see how it effects builds across platforms.
Your answer
Follow this Question
Related Questions
Screen.CursorLock problems 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cursor Lockmode only works once? 1 Answer