TouchTapInWorld
The TouchTapInWorld event fires when the user touches/taps their finger on the screen on a UserInputService/TouchEnabled|TouchEnabled
device. It is fired when the user taps in the game world.
This event can be used to determine when a user taps the screen and does not tap a GuiObject|GUI
element. If the user taps a GUI element, UserInputService/TouchTap
will fire instead of TouchTapInWorld.
To check if a user’s device is TouchEnabled, and that touch events will fire, see UserInputService/TouchEnabled
.
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 it only fires locally, it can only be used in a LocalScript
.
See also
UserInputService/TouchTap
UserInputService/TouchLongPress
UserInputService/TouchMoved
UserInputService/TouchPan
UserInputService/TouchPinch
UserInputService/TouchRotate
UserInputService/TouchSwipe
UserInputService/TouchStarted
UserInputService/TouchEnded
Parameters
Name | Type | Default | Description |
---|---|---|---|
|
A |
||
|
Whether the user tapped a GUI element |
Code Samples
Create a Part in World at Touch Position
This example uses the DataType/Vector2|Vector2
position passed by UserInputService/TouchTapInWorld|TouchTapInWorld
to find the DataType/Vector3|Vector3
world position the user tapped. Then, the code spawns an anchored BasePart|Part
at the world position.
In order to calculate the DataType/Vector3|Vector3
world position using the viewport position, this example generates a Ray
called unitRay originating from position using the Camera/ViewportPointToRay|ViewportPointToRay
function. Then, since ViewportPointToRay() creates a unit ray that is only 1 stud long, the example uses it to create a longer ray that is length studs long. Using Workspace/FindPartOnRay|FindPartOnRay
, the code determines where the ray first intersects a part in the world - the Vector3 world position that the user tapped.
Note that the code sample will not spawn a part if the user touches on the screen over an empty skybox. The touch must be on a part for FindPartOnRay() to return a DataType/Vector3|Vector3
position.
local UserInputService = game:GetService("UserInputService")
local camera = game.Workspace.CurrentCamera
local length = 500
local function createPart(position, processedByUI)
-- Do not create a part if the player clicked on a GUI/UI element
if processedByUI then return end
-- Get Vector3 world position from the Vector2 viewport position
local unitRay = camera:ViewportPointToRay(position.X, position.Y)
local ray = Ray.new(unitRay.Origin, unitRay.Direction * length)
local hitPart, worldPosition = game.Workspace:FindPartOnRay(ray)
-- Create a new part at the world position if the player clicked on a part
-- Do not create a new part if player clicks on empty skybox
if hitPart then
local part = Instance.new("Part")
part.Parent = game.Workspace
part.Anchored = true
part.Size = Vector3.new(1, 1, 1)
part.Position = worldPosition
end
end
UserInputService.TouchTapInWorld:Connect(createPart)