PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

MouseBehavior

UserInputService

MouseBehavior

This property sets how the user’s mouse behaves based on the Enum/MouseBehavior Enum. The default value is Enum.MouseBehavior.Default.

It can be set to three values:

  1. Default: The mouse moves freely around the user’s screen.
  2. LockCenter: The mouse is locked, and cannot move from, the center of the user’s screen.
  3. LockCurrentPosition: The mouse is locked, and cannot move from, it’s current position on the user’s screen at the time of locking.

The value of this property does not affect the sensitivity of events tracking mouse movement. For example, UserInputService/GetMouseDelta|GetMouseDelta returns the same DataType/Vector2|Vector2 screen position in pixels regardless of whether the mouse is locked or able to move freely around the user’s screen. As a result, default scripts like those controlling the camera are not impacted by this property.

This property is overridden if a GuiButton with GuiButton/Modal|Modal enabled is GuiButton/Visible unless the player’s right mouse button is down.

Note that, if the mouse is locked, UserInputService/InputChanged will still fire when the player moves the mouse and will pass in the Delta that the mouse attempted to move by. Additionally, if the player is kicked from the game, the mouse will be forcefully unlocked.

As UserInputService is client-side only, this property can only be used in a LocalScript.


Code Samples


Create a Custom CameraScript

By default, Roblox relies on a LocalScript, described [here][1], to control the user’s camera. However, this script can be overridden with a custom CameraScript. The example below demonstrates how to create a custom script to control the user’s camera using many of the UserInputService events.

The script is broken into two parts:

  1. Mobile camera events, which rely on touch events
  2. Non-mobile camera events, which rely on keyboard input and tracking the user’s movement

First, the camera script needs utility functions to setup the camera and set its Camera/CameraType|CameraType to Scriptable so that the script can control the camera. It also needs a function to update the camera when it moves, rotates, and zooms.

Using touch events allows us to track user input as they interact with the touchscreen on their mobile device. These events allow us to handle camera movement, rotation, and zoom.

The second half of the code sample adds camera support for players on desktop devices. When input begans, the function Input() checks that the state of the input is Enum/UserInputState|Enum.UserInputState.Begin to ignore all keypress inputs other than when the user first presses a key down. When the user presses I and O the camera zooms in and out. When the presses down and moves their left mouse button, the script Enum/MouseBehavior|locks the player’s mouse by changing the UserInputService/MouseBehavior property. The camera rotates according to the mouse’s UserInputService/GetMouseDelta|change in screen position. When the player moves their character, the camera moves with them.

All of the parts discussed above are combined and shown in the code sample below.


Create a Binoculars Script

This example creates a binoculars script that decreases the player’s Camera/FieldOfView|FieldOfView and UserInputService/MouseDeltaSensitivity|MouseDeltaSensitivity when a player with a UserInputService/MouseEnabled|MouseEnabled left mouse clicks. The script also points the player’s Camera towards the DataType/Vector3|Vector3 world position of the mouse click determined by the Mouse|Mouse’s Mouse/Hit|Mouse.Hit.p property.

When the player left mouse clicks again, the player’s camera reverts back to the a custom Enum/CameraType|CameraType with the same field of view and Camera/CFrame|CFrame as before the player zoomed in with the script.

While the player uses the binoculars, the script locks the player’s mouse to the center of the screen by setting the player’s UserInputType/MouseBehavior|MouseBehavior to LockCenter. The player’s camera moves when the player moves their mouse according to the InputObject/Delta|InputObject.Delta property passed by UserInputService/InputChanged|InputChanged indicating the mouse’s DataType/Vector2|Vector2 change in screen position.

In order for this example to work as expected, it should be placed in a LocalScript.