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

Can someone explain the Ray Methods? How do you use it to find a part?

I'm planning on making tools for a game, but it will require using rays and it is very new to me. I read about Rays on the Wiki but the Methods didn't make a lot of sense to me. Can anyone help me with the methods and especially what ClosestPoint is? Also maybe an example script/explanation on how to use it to print the first part it passes through or something because I'm also very confused on how to do that. Thank you!

1 answer

✓ Accepted answer

If you are making a gun or laser or something, you don't need closest point. To make a ray, use Ray.new like so:

ray = Ray.new(origin, direction)

Example:

--studDistance is how far the ray should go
studDistance = 500
ray = Ray.new(Tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * studDistance)

what that does, is it makes a ray with the origin of the tools handle, and takes the mouse click position, and minuses that by the tool handle to give you a vector3 direction, which we do .unit on and times it by how far you want it to go.

A great article on raycasting guns is in this link: http://wiki.roblox.com/index.php?title=Making_a_ray-casting_laser_gun

3 comments

0

Why use CFrame instead of position? Is there a difference?

0

From the wiki, it doesn't look like it

0

CFrame can include the position as well as the rotation

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