How to detect what part a mobile player is touching

I’ve read that it’s not the best to use Mouse.Target. Currently I am raycasting from a 3d point which is created by using ViewportPointToRay. However it appears that doing so either creates some kind of offset or the perspective of the camera makes it seem off.

As you can see here, currently the player has to touch a little below the green parts in order for it to detect they are touching them:


So what is the best way to detect what part a mobile player touches with their finger?

I think you should be able to use a raycast from the mouse position.

Try using UserInputService::GetMouseLocation to get the mouse’s onscreen position then pass it to Camera::ScreenPointToRay and then pass the Ray returned to workspace::FindPartOnRay and get the Part that the Ray hit.

I am already pretty much doing that. The game is designed for mobile devices, so I shouldn’t really be using GetMouseLocation.

UserInputService has a .TouchMoved event that returns an Input object with a Position property representing the “cursors” current position on the screen.

You can then combine this with Camera::ScreenPointToRay

That is exactly what I am doing right now. However, either it is a little offset from the touch location or (more likely) because of the camera’s perspective, it is not precise. You can see it in the gif.

Nevermind, turns out using ViewportPointToRay offsets it. Tried ScreenPointToRay and it works perfect! Sorry for any confusion.

1 Like

Not sure why this happens. I assume it is this:

“This position does not account for the GUI inset. The Ray originates from the Vector3 equivalent of the 2D position in the world at the given depth (in studs) away from the Camera .
As this function does not account for the GUI inset, the viewport position given is not equivalent to the screen position used by GUI elements. If you are not using ScreenGui.IgnoreGuiInset and need an otherwise identical function that accounts for the GUI offset, use Camera:ScreenPointToRay .”

1 Like

Honestly, I’d assume that no gui offset using ViewportPointToRay would mean that there would be no offset. It’s sorta odd. Thanks for the help

1 Like