Right now, the only way to lock the player’s mouse to the center of the screen is by setting the property UserInputService.MouseBehavior
to Enum.MouseBehavior.LockCenter
Unfortunately, when you do this, the actual position of the mouse does not update until you move your mouse again. This can cause a number of annoying bugs, especially if you have UI elements (such as a weapon hotbar) which are responsive to mouse input.
Example A:
This video may be a bit confusing, but in this example my input is as follows:
- Click Slot 3 (MouseBehavior is set to LockCenter since I am equipping a weapon)
- Press the key ‘4’ to select slot 4 (MouseBehavior is freed back to Default)
- Click to place the vehicle; however, since the game still thinks I’m hovering over Slot 3 regardless of where my mouse appears to be, it will select that slot instead.
- I press the key ‘4’ to demonstrate the same thing again.
This is not the input that the player intended, yet because setting MouseBehavior to LockCenter does not internally update where the mouse is positioned (regardless of where it appears to be positioned), the results are unexpected if the player does not manually move their mouse after MouseBehavior is changed.
Another example, which is easier to demonstrate in a video, is that when the mouse is freed by setting MouseBehavior back to Default, and then moving your mouse, your cursor will then snap to where it was before it was set to LockCenter. This can be really unexpected and jarring.
Example B:
Another consequence of this is that the mouse will make a radical jump on the next InputChanged event (when the player moves their mouse), meaning you have to anticipate this mouse movement in camera systems so that their camera does not immediately jump upwards when their mouse locks to the center of the screen in the mouse movement after the MouseBehavior is set. This is a weird idiosyncrasy with the roblox mouse system.
I would like for players of my game to have a smooth and responsive experience, but unfortunately this bug in particular is out of my hands. It would be nice if changing MouseBehavior to LockCenter would immediately update where the player’s mouse is positioned as far as code is concerned, rather than having to wait for the player’s next mouse input.