• 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 AlxAlxAlx · Jan 07, 2018 at 06:38 PM · webglcursorlockcursormouselock

How to lock cursor in WebGL

Hello,

At this point I am quite desperate so I decided to seek help personally. I have this (project) and in my newest build, when I looked around it would snap back ( probably after the cursor reached an edge of the screen) which is weird because that never occured before and I am using unity first person controller. Afterwards I tried to search trough the forum to see what the problem is. Ended up finding this code

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class MouseLock : MonoBehaviour
  {
  
      // Update is called once per frame
      void Update()
      {
          if (Input.GetMouseButtonDown(0))
          {
              //Mouse lock and confine
              Cursor.visible = false;
              Cursor.lockState = CursorLockMode.Locked;
              Cursor.lockState = CursorLockMode.Confined;
          }
  
          if (Input.GetKeyDown("1"))
          {
              //and to unlock it is this:
              Cursor.lockState = CursorLockMode.None;
              Cursor.visible = true;
          }
      }
  }

that I have tried implementing first just by adding it as a component onto the first person camera then the controller. None of that worked so I tried to insert it into the code of the controller, but that didn't help either. And now I am clueless of what to try next.

Any help would be greatly appreciated since I am quite a noob and made the whole project just by tweaking stuff from tutorial videos.

Also to see the original problem see the website after I changed the code it didn't even snap back it didn't even allow me to move any further from which I conclude, that the cursors just turns invisible but it doesn't lock. In my unity editor everything works fine.

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
0

Answer by hunvee3 · Mar 21, 2018 at 01:26 AM

I'm not sure about this since I haven't tested yet but the manual states that for security reasons browsers don't allow any events related to cursor locking that it can't prove came from a user initiated event. By the time you call input.getkeydown it's too late and the browser ignores it. The manual says that cursor locking should be done on up events so you should use getkeyup instead of getkeyup. Hope this helps

Comment
Add comment · Show 1 · 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 Afroworms · Oct 08, 2018 at 05:24 PM 1
Share

I don't know from which documentation you got the fact that mouse/key up events where the way to go, straight from the actual documentation:

you should trigger cursor locking or full-screen requests on mouse/key down events, ins$$anonymous$$d of mouse/key up events

Documentation

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

80 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 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

Mouse won't unlock in WebGL build but does in the editor 1 Answer

Cursor won't stay locked in update!! 0 Answers

Trouble with unlocking cursor 1 Answer

Cursor.lockState does not work 2 Answers

FPS Controller Mouse not locking consistantly 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