Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

WebGL does not capture the mouse correctly

Discussion in 'Web' started by VCC_Geek, Apr 10, 2018.

  1. VCC_Geek

    VCC_Geek

    Joined:
    Apr 30, 2013
    Posts:
    29
    This happens in both Chrome and Firefox. When the game starts, the mouse cursor is inside the window and invisible, as it should be (via the Cursor.lockState and Cursor.visible properties). However, as soon as you start moving the mouse around (in order to look around), the mouse pointer leaves the game window and becomes visible. When it hits the edge of the screen, you can't move it any further. If you click inside the window, then you get a message from the browser that the mouse is captured ("This document has control of your cursor"), and you can play normally.

    My question: is there any way to capture the mouse without expecting the user to click inside the window? As mentioned above, I'm already using Cursor.lockState and Cursor.visible. I also tried adding this as a button that runs the cursor capture code well after the game is loaded.

    Also, if the user presses Esc to release the mouse, then the mouse never locks again - the only way to capture the mouse pointer again is to reload the page.

    Thanks!
     
    Last edited: Apr 10, 2018
  2. VCC_Geek

    VCC_Geek

    Joined:
    Apr 30, 2013
    Posts:
    29
    So I (partially) found my own answer, and honestly, I'm kind of bummed about it. Not Unity's fault, though.

    It's a security thing. The browser won't let you automatically capture the pointer OR go full-screen unless it's in direct response to user input. Not sure I see how on earth this is a security concern, but maybe that's because I'm biased at the moment. It sort of throws a monkey wrench into the works in terms of a seamless experience.

    That does leave the other question, though. When the user presses Esc, the cursor locking is cancelled, and it won't re-lock. I've experienced this even in the editor. Any guidance on that?
     
  3. et3rnald

    et3rnald

    Joined:
    Aug 17, 2017
    Posts:
    4
    I too have a question about this...I published a Unity FPS mini-game to WebGL (https://connect.unity.com/p/unity-fps-game-2). I believe I followed all the directions correctly. however, when i open the link and play, i cannot look around using the mouse beyond a certain point. i realized this is because when the cursor hits the bounds of my computer screen, it stops my looking around at that boundary...please help!
     
    its_ak16 likes this.