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

How can I get the position of where the mouse is touching?

I am attempting to use Ray Casting for the first time and I feel like i'm missing something simple. Attached is my current code. At the moment the part returned is nil and the position does not seem to be correct. If anyone could give me a link or pointer that would be great! Thanks.

player = game.Players.LocalPlayer
mouse = player:GetMouse()

function camTurn()
	local camRay = mouse.UnitRay
	
	basePart, location = workspace:FindPartOnRay (camRay)
	print (basePart .. " Is touched here: "..location)
end

RunService:BindToRenderStep("CamTurn",500,camTurn)--I have tried lower priority as well

1 comment

0

Are you trying to find the position of a part that the mouse is hovering over or?

1 answer

Your code doesn't work because rays require some length for hit detection but UnitRay is only of length 1. (Unit).

However I do not recommend Lua raycasting every render frame, plus for no reason. You can simply use mouse.Target / mouse.Hit.

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