Cursor can't click in Locked mouse state

I want my mouse cursor always in the center of my screen, but also I want to be able to click UI buttons.

When I use cursor.Lockstate = CursorLockmode.Locked it stays in the middle of the screen, but it doesn’t register hits anymore.

I tried a little bit with raycasts to detect if I hit a UI button, but I can’t get it to work, because it doesn’t detect the buttons.

Is there a way to get both keep the mouse cursor in the middle of the screen, but also be able to click UI buttons.

1 Like

Override the Standalone Input Module with your own logic, here’s an example: Replacement for Unity's StandaloneInputModule, world-space UI now reacts to first person locked cursor, screen center being the pointer position. · GitHub

Does it still work? When I add it to the Event System it puts a visible cursor in the middle of the screen but it still doesn’t interact with the ui

1 Like

The linked code works. Do you have a crosshair as a Unity UI Image? Turn off the Raycast Target on it since that Image might be receiving mouse clicks and preventing input from being used for whatever’s underneath the crosshair.

The only problem with the code posted is that it makes the cursor visible even while locked on the center. It seems rapidly changing Cursor.lockState within the same frame doesn’t really work as expected.

UnityEngine.EventSystems.PointerInputModule, unfortunately, just assumes that we always want the UI to be disabled when Cursor.lockState == CursorLockMode.Locked, it doesn’t provide an option to circumvent that.

Only way I was able to fix it is to copy-paste the underlying code of GetMousePointerEventData, ProcessMove, and ProcessDrag into that custom FirstPersonInputModule (instead of just calling the base methods), then removing the code that disables functionality when Cursor.lockState == CursorLockMode.Locked.

It’s not really an advisable thing to do, since it makes the FirstPersonInputModule potentially become incompatible if a version update of Unity UI ever comes that changes the code inside PointerInputModule.

1 Like

I’m currently stuck on this issue. Could you please explain a bit more what you mean by this?
The UI now works but the cursor is visible.

Having a bit of confusion what you did to fix, I’m not sure how to approach this fix.

Sorry for digging this out but I came across this script and found that it makes cursor show and hide constantly and also influence the movement smoothness. Solution is to change CursorLockMode.None to CursorLockMode.Confined

1 Like

IPointerHandler behaviour is problematic when you’re locking your cursor because it by default always put the cursor outside the screen. With the new input system and …? version you can fix that.

9844515--1416642--Problem.jpg

1 Like

If anyone is looking for the updated version of the script, still using old input system.

This avoids the flickering cursor issue , many thanks to @nosferathoo for pointing out this fix**.**

1 Like