I’ve been browsing around the wiki a bit and can’t seem to figure out how to find all locations of current touch inputs without having to manually do some stuff w/ InputBegan which is hacky at best. Ideally I’d like to know when a touch input begins & where it drags to, and the moment it stops being touched. These are all things you can do w/ InputBegan/Changed/Ended but that has edge cases/inaccuracies where you can’t guarantee that the same finger is causing the Began/Changed/Ended events. Is there some sort of API I can’t find that does this or something?
5 Likes
InputObjects persist for the lifetime of the “finger” on the screen. So you can determine state by storing the start input object and passing it down in a function to compare to the end event. You can quite easily make a global tracker if you needed to.
Comparisons like startInputObject == inputObject is quite normal for me.
Not sure if there’s an easy way to retrieve the global state of touch input objects tho.
edit: should this be in the scripting helpers forum?
7 Likes
^ this.
Here’s how I’d probably go about doing it (though I’d advocate for just using the events):
local UserInputService = game:GetService("UserInputService")
-- set of all currently active touch points
-- indexes are InputObjects
-- values are Vector3s
local touchPoints = {}
UserInputService.InputBegan:Connect(function(inputObject, gameProcessed)
if gameProcessed then return end --optional
if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
touchPoints[inputObject] = inputObject.Position
inputObject.Changed:Connect(function()
touchPoints[inputObject] = inputObject.Position
end)
end
UserInputService.InputEnded:Connect(function(inputObject, gameProcessed)
if gameProcessed then return end --optional
if inputObject.UserInputType ~= Enum.UserInputType.Touch then return end
touchPoints[inputObject] = nil
end
5 Likes