How do I raycast in 2021?

Guys, I have been searching on youtube for a tutorial on how to raycast. Most tutorials are old and a lot of stuff is deprecated. The newer videos just dont explain it properly and are to advanced. I am a big noob at scripting. I really need a very detailed explanation and tutorial about how to raycast.

5 Likes

Try these
1
2
3

2 Likes
  1. Get a starting position

  2. Get a direction vector (No, not position. Direction. Like,
    (goal.Position - start.Position
    OR
    -start.Position + goal.Position))
    (Use ‘Vector Triangle Method’ to visualize a direction vector)

  3. Get a RaycastParams object (Optional, raycast still works without this)

local rayParam = RaycastParams.new()
rayParam.FilterDescendantInstances = {workspace.IgnoreFolder}
rayParam.FilterType = Enum.RaycastFilterType.Blacklist
rayParam.___ -- autocomplete will tell you what you can add more
  1. Raycast (don’t leave out the local ray = ___, you’ll use them later)
local ray = workspace:Raycast(startPosition, directionVector, rayParam)
  1. Hit detection
if ray then
    -- Some properties you can get
    ray.Instance -- The part hit (can return Terrain)
    ray.Position -- The position the hit occured
    ray.Material -- The material of the Instance (works for terrain too)
    ray.Normal -- LookVector, except it's the direction from the face of a part
end
23 Likes