Is there any reason why GuiObject.AbsolutePosition would be inaccurate?

I’m creating a box holding script that I’m now trying to make mobile compatible. For mobile devices, on a screen tap I send a raycast based off of a GuiObject reticle in the center of the screen to determine of a box is being clicked on. However, I’m noticing that the position I’m getting is off, being slightly upwards.

Code:

UserInputService.TouchTapInWorld:Connect(function(touch, processedByUi) -- mobile detection
	local pos = Reticle.AbsolutePosition -- reticle positioned in center of screen
	local unitRay = workspace.CurrentCamera:ViewportPointToRay(pos.x, pos.y)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction*100)
	local hitPart = workspace:FindPartOnRay(ray)
	
	handleBoxClick(hitPart)
end)

GIF of problem: https://streamable.com/g9tkbs

As you can see, I need to position the reticle around half way down the box for it to detect it, and if the reticle is slightly in front of the box, it will still be detected. This GIF is from studio’s mobile emulator, but I can confirm that the issue persists in game from my phone.

Is there any reason why the position I’m getting is inaccurate? And how would I fix this?

Try using ScreenPointToRay instead, ViewportPointToRay doesn’t account for gui inset.

1 Like