Are you tired of dealing with the weirdness of cursor capture in Unity WebGL builds? I know I was. Sometimes it seems like the cursor wouldn't act as you expected, causing it to leave the frame or stop at the edge, despite how many times you set Cursor.lockState = CursorLockMode.Locked...

This quick test shows that the browser will quietly eat some inputs and leaves the game in an unexpected state. The game initially sets CursorLockMode.Locked in a Start function, but the cursor won't actually get captured until the user clicks in the frame, as mentioned here. Best I've been able to determine, the cursor will only become locked when the state changes to Locked, simply setting Locked when Unity thinks it's already Locked won't do anything. Clicking RMB shows this to be the case, as it toggles between None and Locked and succeeds in locking the cursor even when the game is in an inconsistent state.

The box on the left lists the inputs received as well as their time. The two boxes on the right show the current cursor state (according to Unity) and the mouse position/delta.

Controls

  • Escape: CursorLockMode.None, eaten by browser if the cursor is currently captured or the game is in full screen.
  • E: CursorLockMode.None, not eaten by browser.
  • LMB: CursorLockMode.Locked
  • RMB: CursorLockMode.None immediately followed by CursorLockMode.Locked
StatusReleased
CategoryOther
PlatformsHTML5
Authorcottontshirtz
Made withUnity

Leave a comment

Log in with itch.io to leave a comment.