Can't lock the cursor!

I’m working on a little project for my job and I need to do a First Person Shooter kind of view. I made the controls and everything, applied the Cursor.lockState = CursorLockMode.Locked; to my code and everything worked fine in editor.

But then, when I exported to WebGL an tested it on the server, the cursor didn’t lock.

I did some search and found this page on the documentation that says I can only do cursor lock/fullscreen commands on broswers with an user initated command. So I staterd to do some tests.

And I simply can’t get it to work.

Here’s what I already tried:

  • On Update, put the Cursor.lockState = CursorLockMode.Locked; inside a if(GetMouseButtonDown(0));

  • On Update, put the Cursor.lockState = CursorLockMode.Locked; inside a if(GetMouseButtonUp(0));

  • Follow the example of the documentation and extend the UI.Button class, overriding the OnPointerDown method, calling Cursor.lockState = CursorLockMode.Locked; inside this method;

  • Just for tests, I did the same thing as above, but overriding the OnPointerUp method;

And none of these worked.

Can someone tell me how the hell do I lock the cursor on a Broswer using Unity??

NOTE: I tested it on Chrome and Firefox, using Unity 2019.4.8f1

use “Screen.lockCursor”. This will work.

So, I actually just literally found what was wrong with my project. So Cursor.lockedState is working now.

The thing is that I was using a CUSTOM Input Module that I took from the internet some weeks ago (honestly, I don’t even remeber why I needed that), and the person who wrote it was messing with the Cursor.lockedState. I deactivated this custom input module and added the stardrt StandaloneInputModule to test, and guess what? The cursor finally locked as expected!

So, I guess the lesson here is: Be careful with random code you get from the internet, kids. It might end up making you spend almost a week trying to figure out why your game doesn’t work.

1 Like

Hello

I have a similar issue with cursor and WebGL. My app is juste a camera controlled by mouse movement in order to watch the scene (whitout moving player) and I have some objects where I can click to interact with using OnMouseDown. I have a crosshair in center of my screen to point and click. I lock cursor with :
Cursor.lockState = CursorLockMode.Locked; inside a if(GetMouseButtonDown(0)); like you did.

First, I thought cursor was not locking but finally cursor is well lock but it is lock in current position so I have an offset between my crosshair and cursor.

I tested on 2019.4 and 2020.3 but same issue.

Do you have any idea ?

Hello,

I’m in version 2021.2.4f1 now and watched the Unity tutorial from Brackeys:
…/watch?v=_QajrabyTJc&t=832s

Well, the problem with the screen locking was, that I tried ANITHYING I could think of, but it didn’t work!

Later, I clicked on the screen and then it worked…
Why? Because I started it, the game view opened and I thought that was everything that I needed to do ):
The answer is that the Game view popped up, but I didn’t select it.

So the ANSWER:

Select your game view first, that your game locks the cursor!

Hope it helped,
Nice day :slight_smile:

2 Likes

Hello!
My Cursor dose not lock when i tried the code: Cursor.lockState = CursorLockMode.Locked; What is the problem?
I tried copy pasting the code from someone, but did not work. :frowning:

I guess you are using webgl. I would say webgl is special platform that has many strict security. One of them is, it could only lock a cursor from user interaction event. In browser like chrome it was restrict to some seconds after user press some key or mouse button. Normally unity hookup the mouse lock on user clicking the canvas. But if you try to force pointer locked from the code it would not work

Also there is something about permission popup that you must never miss. Before any site can lock the mouse, the browser will try to ask for mouse lock permission. And if you denied it once it would be hidden and need to find a place to reset site setting manually

1 Like

Alternatively we should access the actual state of locking to determine what we should do. Which need to be a bit hacked with jslib

I try to propose that this should be support in unity directly