- Home /
Mouse won't unlock in WebGL build but does in the editor
I'm using this code: (in an Update function)
if (Input.GetKeyDown (KeyCode.Escape)) {
unlock = true;
}
if (unlock) {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
Screen.fullScreen = false;
} else {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
if (Input.GetMouseButtonDown (0)) {
unlock = false;
}
In the editor, it works as expected. For whatever reason though, in the actual WebGL Build, the cursor does not unlock. I've searched thru every script in my project, this is the only code affecting the mouse lock, so I'm confused as to why the mouse won't unlock. Could someone help me out please?
Edit: I tried building the project as a standalone app, and it works perfectly. This leads me to believe it is a bug with WebGL, has anyone else come across this bug and figured out how to fix it? I need this app to be able to work on webpages.
Answer by Bunny83 · Sep 20, 2016 at 10:01 PM
Do not lock the cursor every frame. I would guess that you simply can't detect the escape key as the escape key is already handled by the browser to automatically unlock the cursor. However since you re-lock the cursor everyframe you can't unlock it...
So strictly speaking it's not a bug but simply misusing of the API. However it shouldn't be possible to completely lock out the user. So if you classify it as bug, it's a bug in the browser implementation. The browser could require a user interaction after the esc button has been pressed before it allows to re-lock the cursor. What browser do you use?
I changed my code to this
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.P)) {
Cursor.lockState = CursorLock$$anonymous$$ode.None;
Cursor.visible = true;
Screen.fullScreen = false;
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R)) {
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.visible = false;
}
and now it does work as it should. That's odd that calling it every frame would matter, since I was using an if / else statement it should be impossible for it to call both the lock and the unlock methods at the same time, right? Though only calling it once did fix it so what can I say... Thanks!
What i ment is, have you actually checked if you enter this if statement:
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Escape)) {
unlock = true;
}
I guess that you don't even set "unlock" to true because the browser will eat the ESC press. All browsers will automatically cancel fullscreen mode and release the cursor when you press ESC.
Interesting, so basically in short: Escape key doesn't work well with WebGL?
Dor example i use FireFox and i have my VisPortals example. There i lock the cursor on right mouse button down and release it on mouse up. So while you're holding it down it should be locked. However FireFox only locks the cursor when i press any button after locking.
If i press ESC while holding down the right mouse button, the cursor is still hidden, but it's unlocked and i can move it out of the canvas where it's visible.
Your answer
Follow this Question
Related Questions
How to lock cursor in WebGL 1 Answer
Can you lock the cursor to specific coordinates? 0 Answers
WebGL Cursor Lock not centering 1 Answer
Standalone problem with cursor 2 Answers
Cursor won't stay locked in update!! 0 Answers