Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Mouse.UnitRay doesn't work properly?

Asked by
Griffi0n 315 Moderation Voter
6 years ago
Edited 6 years ago

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

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago

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)
Ad

Answer this question