The TouchRotate event fires when a user rotates two fingers on a
For example, the following code prints how much the camera has rotated since the beginning of the touch rotation.
local UserInputService = game:GetService("UserInputService") UserInputService.TouchRotate:Connect(function(touchPositions, rotation, velocity, state, gameProcessedEvent) print("Camera has rotated "..tostring(rotation).." degrees!") end)
To check if a user’s device is TouchEnabled, and that touch events will fire, see
This event only fires when the Roblox client window is in focus. For example, inputs will not be captured when the window is minimized.
As this event only fires locally, it can only be used in a
The core scripts that control the user’s camera on a mobile device use code that functions similarly to this event. For more info on these scripts, see here. Best practice for this event is to use it when creating a mobile camera system to override the default core scripts.
An array of
The number of degree the gesture has rotated since the start of the gesture
The change in rotation (in degrees) divided by the duration of the change (in seconds)
Indicates whether the game engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input,
Create a Custom CameraScript
By default, Roblox relies on a
LocalScript, described [here], 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
The script is broken into two parts:
- Mobile camera events, which rely on touch events
- 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.