My Intro to Raycasting - WHAT, WHY, and HOW

My Intro to Raycasting

Hello, and today I will teach you how to use raycasting, as well as many common use cases of it in Roblox development. Raycasting is a big part of scripting.

Raycasting - WHAT, WHY, and HOW

WHAT

What is raycasting? Well, Raycasting is making a line between two points and seeing what object the line intersects with first on it’s travel. Well, here is the basics of how it works:

local origin = Vector3.new(0,0,0)
local direction = Vector3.new(0,10,0)
local raycastResult = workspace:Raycast(origin,direction)

This will raycast from the center of the world to a point above the center of the world. If it finds anything, it will return a RaycastResult object. It tells you a lot of what it found, like the material of the object found, and the object itself. However, what is Raycasting usually used for?

WHY

What is raycasting used for? Well, let me tell you! It is used for:

  • Making guns
  • Checking if an NPC can see a player
  • Making sure things don’t go through walls
  • Knifes sometimes
    This is only the tip of the iceberg of what it is used for. I highly recommend you look into more.

HOW

What about the how? How do we use raycasting, especially it’s more advanced features? You saw an incredibly simple example earlier, and that was just the basics. It didn’t even attempt to use RaycastParams, so I’ll go ahead and tell you what their job is.

RaycastParams are a way to change how the raycast operation in performed. To start using RaycastParams, begin by declaring a variable and creating the RaycastParams object.

-- Is declaring a new RaycastParams object
local rParams = RaycastingParams.new()

Next, you change it’s properties. For an example, let’s change it so it only looks for certain objects to collide with.

local rParams = RaycastingParams.new()
-- Sets the FilterType property of the object to Whitelist
rParams.FilterType = Enum.RaycastFilterType.Whitelist

Now, we need to pass it what objects should be counted.

local rParams = RaycastingParams.new()
rParams.FilterType = Enum.RaycastFilterType.Whitelist
-- Sets the FilterDescendantsInstances property of the object to a certain folder in the workspace
rParams.FilterDescendantsInstances = {workspace.Trees}

Then we now have the ability to raycast and make it collide with only certain objects! To actually use the RaycastParams, you do this:

local rParams = RaycastingParams.new()
rParams.FilterType = Enum.RaycastFilterType.Whitelist
rParams.FilterDescendantsInstances = {workspace.Trees}

local origin = Vector3.new(0,0,0)
local direction = Vector3.new(0,0,50)
-- You pass the RaycastParams as the third value in the function
local raycastResult = workspace:Raycast(origin,direction,rParams)

To make raycasts avoid detecting certain objects, you would change the FilterType property like this:

rParams.FilterType = Enum.RaycastFilterType.Blacklist

Conclusion

Raycasting is a really useful tool when it comes to Roblox scripting, almost necessary. It gives us a way to do many advanced things that would not be possible otherwise, like FastCast and raycast-simulated bullet physics. It is also just regularly used to make hitscan weapons for extra accuracy. I hope you learned how raycasting works and especially it’s it’s more advanced uses. Anyways, goodbye, and happy scripting!

93 Likes

How did I do with the tutorial?

16 Likes

Thank you so much for the tutorial, I was struggling to understand raycast a little bit now I have a more clear idea!
Though I have a question if you don’t mind me asking what is the difference between region3 and raycasting?

1 Like

Region3 is for looking for parts in a volume of space. Raycasting is looking for objects that intersect with a line.

1 Like

small error, it should be

RaycastParams.new() not RaycastingParams.new() i think

good tutorual

10 Likes

You should definitely make a video tutorial!

Or something where you construct things like the little game on the Roblox tutorial I’ve needed help with it!

This game idea looks like it would be of significant value to yourself, do this one!

1 Like

Very Helpful Thx for the Tutorial.

3 Likes

I still don’t understand raycasting, even after reading this. I’ve been trying to use raycasting for a game and for FOUR HOURS, YES, FOUR HOURS, it isn’t working after endlessly trying to fix it. If I can’t get raycasting to work then I can’t get the game to function properly, and 4 weeks of work will be wasted most likely.

2 Likes

what problems are u facing ?
like does it not show the raycast result u expected ?

1 Like

Using Region3 for detecting parts is deprecated method since we have OverlapParams. Although Region3 can still be used for method FillRegion in Terrain

1 Like