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!