How to script image label gui crosshair?

I want a ray to be casted from the characters head, to the position of the crosshair. This is on mobile
Basically its like Mouse.Hit.P but instead of a mouse its a gui. ImageLabel.Hit.P something like that.

I couldn’t really find something but I still found this: Camera | Roblox Creator Documentation

if UserInputService.TouchEnabled == true or UserInputService.KeyboardEnabled == false then
				local MobileRayPosition = Camera:ScreenPointToRay(Crosshair.AbsolutePosition.X, Crosshair.AbsolutePosition.Y) 
				local MobileRay = workspace:Raycast(Head.Position, MobileRayPosition.Direction * 5000 ,RayParams)
				local P
			
				if MobileRay then
					 P = MobileRay.Position
				else
					 P = Vector3.new(CFrame.new(Head.Position, MobileRayPosition.Direction))
				end
				CastRay(P)
			else
			
			
				local P = Mouse.Hit.p
		
	
				CastRay(P)
		end

Can you help me fix my code? For some reason its not going to the gui crosshair.

I would not use Crosshair.AbsolutePosition.xy I would use Crosshair.Position.xy

If you want to replicate Mouse.Hit.Position then I would make the origin the CurrentCamera’s Origin

And you can also use Mouse.Hit.Position for mobile players

i can use Mouse.Hit.Position for mobile but i dont want it to be where the user tapped, i want it to be where the crosshair is on the screen

edit: i got it to work. I referenced the gui not the text label. Now it works but its not exactly where the crosshair is. Its a bit on top or below.

1 Like

https://developer.roblox.com/en-us/api-reference/function/Camera/ViewportPointToRay

Provide it the crosshair’s on-screen X & Y position.