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

How to cast a ray, then detect an object that comes in contact with the ray?

Asked by 3 years ago

I've looked many places, I've seen many discussion threads, and have watched many tutorials, I am still just as confused. I tried my hand at it, but predictably I failed. So can someone explain to me like im a 5 year old how to ray cast? Also is there anyway to make a casted ray visible (for testing purposes obviously)?

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Imagine Raycasting like a laser you would buy for your cat... And then imagine your cat attacking the red dot on the floor like crazy. Now let's convert that into code.

local Raycast = Ray.new(Vector3.new(), Vector3.new(0, 0, 5))
local HitPart, HitPosition = workspace:FindPartOnRayWithIgnoreList(Raycast, {workspace.Baseplate, workspace.StupidWall})

Now let me explain. The "Vector3.new()" is the position that the ray starts at. And the "Vector3.new(0, 0, 5)" is the direction that the ray is shot at. So if the direction was Vector3.new(0, 5, 5) the ray would be shot at an angle upwards. Then we check then we check if the ray hits something with workspace:FindPartOnRay add an ignore list by putting "WithIgnoreList" after. Then we get the hit part and hit position. If you need me to explain it even more feel free to ask.

0
Id accept your answer, but for some reason the site is not letting me. generalYURASKO 144 — 3y
Ad

Answer this question