PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

GetMouseDelta

This function returns the change, in pixels, of the position of the player’s Mouse in the last rendered frame as a DataType/Vector2. This function only works if the mouse has been locked using the UserInputService.MouseBehavior property. If the mouse has not been locked, the returned DataType/Vector2 values will be zero.

The sensitivity of the mouse, determined in the client’s settings and UserInputService/MouseDeltaSensitivity, will influence the result.

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

Returns

Return Type Summary

Change in movement of the mouse


Code Samples


Getting Mouse Delta

GetMouseDelta returns the current change in movement of the mouse as a Vector2, but only if the mouse is locked. If the mouse isn’t locked the values in the returned Vector2 will be zero. It measures any mouse movement in pixels from the last render step to the current render step. If the user has set their camera sensitivity to be higher or lower than 1 in the in-game menu this will affect the value returned by GetMouseDelta. The camera sensitivity is a multiplier to the amount the camera moves as a result of mouse input.


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.