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

How can I find the exact position of where a Ray hits a part?

I'm trying to find the exact position of where a ray hits a part. I first tried taking the Y position and the Z position of the rays origin and the X position of the part to find the exact position hit, but ofcourse this method won't work unless the ray is casts along the x axis. I need to be able to have rays at all different angles and find the exact position of the of where the ray hits the part.

local MovingPart = game.Workspace.TestPart --Part I want to move to the position hit
local finish = script.Parent.finish
local start = script.Parent.start
local rayOrigin = start.Position
local rayDestination = finish.Position
local rayDirection = rayDestination - rayOrigin
local ray = Ray.new(rayOrigin, rayDirection)
local part = workspace:FindPartOnRay(ray)
local ypos = start.Position.y
local zpos = start.Position.z
local xpos = part.Position.x

MovingPart.Position = Vector3.new(xpos, ypos, zpos)

1 answer

✓ Accepted answer

Use raycast instead of ray, it returns a RaycastResult which has RaycastResult.Position The world space point at which the intersection occurred, usually a point directly on the surface of the instance.

1 comment

0

Thank you!

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