Mouse Hit but with a frame

I have a frame acting as the players crosshair, and I wanna get the same result as a Mouse.Hit.Position but with a frame instead.

Heres what I’ve got:

function crosshairPosition()
	local RAY_DISTANCE = 1000
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {character}

	local crosshairPos = {crosshairFrame.Position.X.Offset, crosshairFrame.Position.Y.Offset}
	local viewportMouseRay = currentCamera:ViewportPointToRay(crosshairPos[1], crosshairPos[2])
	local rayCastResult = workspace:Raycast(viewportMouseRay.Origin, viewportMouseRay.Direction * (RAY_DISTANCE), raycastParams)
	local hitPosition
	
	if rayCastResult then
		hitPosition =  rayCastResult.Position
	end
	
	return hitPosition
end

I added a print to my script and this is what I get:


Top is the frame method and the bottom is Mouse.Hit

My problem is that its not the exact same, please help.

1 Like

Try to replace the mouse icon not to make a new GUI for it, so it should be worked.

I have a mechanic that automatically aims (placing the frame’s position on an enemy) for mobile players. Also, the crosshair is animated when the player shoots. So this is not an option sadly :pensive:

Are you sure that GUI is correctly set on the middle of the screen, and the icon’s middle is on the GUI’s middle?

I don’t understand what you’re asking. If your asking if the crosshair is at the center of the curser then yes.

I got it :sob:

This is in a top down camera script and I had the function assigned to a variable, and that variable was before the camera was set to the top. So the function works, I’m just stupid :slightly_smiling_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.