Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by mightybob · Sep 20, 2016 at 09:42 PM · webglcursorunlocklockcursormouselock

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mightybob · Sep 20, 2016 at 10:08 PM 0
Share

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!

avatar image Bunny83 mightybob · Sep 20, 2016 at 10:16 PM 0
Share

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.

avatar image mightybob Bunny83 · Sep 20, 2016 at 10:20 PM 0
Share

Interesting, so basically in short: Escape key doesn't work well with WebGL?

avatar image Bunny83 · Sep 20, 2016 at 10:12 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges