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

Mouse.UnitRay doesn't work properly?

At least I think. Here's the code I'm using

-- I already defined the mouse and player variables and yes I defined the correct ones
mouse.Move:connect(function()
	local part, position = game.Workspace:FindPartOnRay(mouse.UnitRay, player.Character)
	print(part, position)
end)

It always prints nil even when I'm pointing to the Baseplate and gives the position of the Camera which doesn't work either. Is this intended behavior of the UnitRay?

1 answer

This will work as long as the target is within 500 studs of the camera.

mouse.Move:Connect(function() -- connect is deprecated, use Connect (uppercase C)
	local mouseray = Ray.new(mouse.UnitRay.Origin,mouse.UnitRay.Direction * 500)
	local part, position = game.Workspace:FindPartOnRay(mouseray, player.Character)
	print(part, position)
end)

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