Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Finding the position of a touch in 3d space?

I was creating a game where you sort blocks based on color, dragging them into boxes. The dragging was easy to do with the mouse as i could check for the mouse.Hit and set the vector3 of the boxes every time the mouse moves (and checking if button1down is down with an if statement), making them draggable.

Recently I have decided to make my game tablet- compatible, but I couldn't find a touch.hit or something like that anywhere . I looked at Input Pan but it only seemed to have a touch position in Vector. I needed the position in 3d space, vector3. I was wondering if anyone could come up with a way to achieve this and if so if they could help me out? Thanks.

1 answer

✓ Accepted answer

Well there is no touch 3D position provided for you, as far as I know. But luckily roblox recently added bunch of helper functions, that make finding 3D position from a 2D point very easy. The function is: camera:ScreenPointToRay(x, y), where x and y are the 2D coordinates on screen and it returns a ray that is 1 stud long. If you'd extend this ray and check if it hit anything, you'd get basically the same thing as mouse.Hit.

local touchPos -- assuming this is the touch position
local maxRayDistance = 1000 -- Each ray has a distance limitation, so this is necessary

local uRay = game.Workspace.CurrentCamera:ScreenPointToRay(touchPos.x, touchPos.y)
local ray = Ray.new(uRay.Origin, uRay.Unit * maxRayDistance)

local touchTarget, touchHit = game.Workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character)

-- Here you have it, equivalent to mouse.Target and mouse.Hit(except touchHit is vector3 not CFrame)

This discussion is preserved as it was. New answers, comments, and votes are closed.