PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

TouchPan

The TouchPan event fires when a user drags at least one finger on a UserInputService/TouchEnabled|TouchEnabled device.

This event can be used to determine when a user pans their finger along screen of a TouchEnabled device - such as to rotate the Camera in a custom camera script.

The snippet below prints “Speed of touch drag” followed by the velocity of the user’s touch when the user drags their finger on the screen.

local userInputService = game:GetService("UserInputService")

userInputService.TouchPan:Connect(function(touchPositions, totalTranslation, velocity, state, gameProcessedEvent)
	print("Speed of touch drag: "..tostring(velocity))
end)

Take a look at another useful UserInputService function here UserInputService/TouchRotate.

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 LocalScript.

See also

  • UserInputService/TouchTap
  • UserInputService/TouchTapInWorld
  • UserInputService/TouchLongPress
  • UserInputService/TouchMoved
  • UserInputService/TouchPinch
  • UserInputService/TouchRotate
  • UserInputService/TouchSwipe
  • UserInputService/TouchStarted
  • UserInputService/TouchEnded

Parameters

Name Type Default Description

An array of DataType/Vector2s, indicating the positions of the touches (e.g. fingers) involved in the gesture

The size of the pan gesture from start to end (in pixels)

The speed of the pan gesture (in pixels) per second

The Enum/UserInputState of the gesture

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, gameProcessedEvent would be true. This is also true for input events connected via ContextActionService


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.