- Home /
CursorLockMode.Locked doesn't hide if cursor hovers the editor
I'm trying to capture the cursor to play in first person, but when I use Cursor.lockState = CursorLockMode.Locked
the cursor gets locked to the center but is still visible, which I don't want.
It took me a while to figure out what was going wrong, I must have my cursor hover the "Game" editor window, only then will the cursor be hidden, otherwise it only gets locked. When running from a build it works without issue.
My question is can I fix this, where I wouldn't have to keep my cursor on the game window?
I had the same issue on 2019.4.3f1 and 2019.4.4f1.
EDIT: I just tried to create a couple new projects on 2019.4.3 and 2019.4.4, and I think my problem is with either the packages or the template I'm using.
I made a custom template (alternative to 3D or "3D With Extras"), basically "High Definition RP" but with less cluther in the assets and with InputSystem and Cinemachine.
I created an extra project with that template, and added a single script with Cursor.lockState = CursorLockMode.Locked
in Start(), and it had the same problem.
I then created two more projects on 2019.4.3 and 2019.4.4, using the regular 3D template, and it worked as it should, the cursor gets captured no matter where the cursor is.
I did have issues on this template with Cinemachine not having a menu at the top, and I had to remove and re-add the package to fix this, so this might be something similar with another package.
EDIT2: I narrowed down the issue to InputSystem, if I create a brand new project, install InputSystem and set the Active Input Handling to Input System, it will reproduce the error. I tested it on 2020.1.f1 and 2019.4.4f1, with template 3D and URP. Setting Active Input Handling to Both seems to fix the issue.
I also sent a bug report.
I found a forum thread which describe pretty much the same issue, but the bug report linked to is slightly different (Says locking doesn't work and only on the Player).
I'm not sure if this is what you need but there is Cursor.visible = false;
At least I use it always and had no problems.
I forgot to mention that I did try Cursor.visible, when I start the game hovering the "Game" window, it works normally, but when I'm hovering somewhere else Cursor.visible = false
has no effects.
Which makes sense because the Unity docs mentions Cursor.lockState = CursorLock$$anonymous$$ode.Locked
sets visible to false by itself.
I'm not really sure what you mean by "hover". The code should work by itself. I'm not sure, but I think it could be that you think it's not working because you're just trying the game in the unity editor. The mouse locking doesn't function the same way when you build the game (It's not a problem or a mistake, it's just to help you edit the game better).
Try building the game and trying it out, if you still think it's not working as you want, then there's a problem. Otherwise, it's just because you're technicality still in the editing process of the game.
By hover I mean, I usually use the standard editor layout, with Hierarchy on the left, Game and Scene in the center, Project and Console at the bottom, etc, and when I launch Play $$anonymous$$ode, the editor buffers for a second and then launch the game, where my cursor should disappear and I'm able to control the game. However, with this issue, if my cursor is over the Hierarchy, the Inspector, the Console or anywhere that's not the "Game" window while the editor buffers, the cursor will only get centered on the "Game" window but still visible. If the cursor is hovering the "Game" window, everything goes on as normal.
But as I added in an edit, I'm pretty sure the problem is with one of my packages. Creating a new project with the "3D" template did not replicate the issue, but if I create one with my custom template, the issue is still there.
So you mean, it works normally but if you move the mouse, even while the cursor is in the center and invisible, if you move it enough it will get over the other windows and the locking of the cursor will stop? You mean that it stops locking, but do you press escape or something like that?
Sorry my brain is half dead so I'm not getting what's happening exactly
Oh and where do you have the "Cursor.lockState = CursorLock$$anonymous$$ode.Locked"?