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

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

Asked by 6 years ago

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

0
Are you trying to find the position of a part that the mouse is hovering over or? gmatchOnRoblox 103 — 6y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
6 years ago

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.

Ad

Answer this question