A simple guide to RayCasting

Here is a video on the subject if you would prefer to watch it instead:

Unsure on how to get started with raycasting? You’re in the right place. This is a simple example of how you can create a simple scene and create a raycast to pass through a part and hit another on the other side.

Here is what the setup looks like:

Here is a simple key for reference:

  • Purple = Origin
  • Red = Obstacle (what we want the ray to pass through)
  • Grey/white = Target (what we want the ray to hit in this example (you won’t need this in most use cases but it is good for a simple example))

First, we need to create the parameters for our raycast:

local Origin = workspace:WaitForChild("Origin") -- The part at which our raycast will begin
local Obstacle = workspace:WaitForChild("Obstacle") -- (An example for how to exlude parts)
local Target = workspace:WaitForChild("Target") -- (Not needed outside of this test)

local Parameters = RaycastParams.new()
Parameters.FilterType = Enum.RaycastFilterType.Exclude -- Allows us to specify parts to ignore
Parameters:AddToFilter({Obstacle}) -- Adds the part for the ray to ignore

Next we can **create a variable the direction** of the ray. This will determine: - The **distance** it travels *(the max distance it is able to travel without hitting anything)* - The **direction** it travels in

For this scene, I will make it travel 100 studs in the Z axis in the direction of the Target:

local Direction = Vector3.new(0,0,100)

Tip:
If you want it to travel forwards from the front face of a part, you can multiply the look vector of it’s CFrame by the distance you want the ray to travel as shown below:

local Distance = 100
local Direction = Origin.CFrame.LookVector*Distance

Now we can carry out the raycast function inside of a pcall function incase it errors:
local Success, Result = pcall(function()
	return workspace:Raycast(Origin.Position, Direction, Parameters)
end)

This will return True and information about what it hit if the raycast succeeds and it will return False and an error message if the raycast fails


Finally, we can check if the raycast succeeded by using an if statement and for this example, we will print success if the part it collides with is our Target.

It can also be useful to use the warn function to notify you of the error that returns when/if the raycast fails.

if Success and Result then
	local Object = Result.Instance
	if Object == Target then
		print("Success!")
	end
else
	warn(Success, Result)
end

Here is the full code if you need it:

local Origin = workspace:WaitForChild("Origin")
local Obstacle = workspace:WaitForChild("Obstacle")
local Target = workspace:WaitForChild("Target") -- Testing purposes

local Parameters = RaycastParams.new()
Parameters.FilterType = Enum.RaycastFilterType.Exclude
Parameters:AddToFilter({Obstacle}) -- Add anything to avoid

local Direction = Vector3.new(0,0,100)

local Success, Result = pcall(function()
	return workspace:Raycast(Origin.Position, Direction, Parameters)
end)

if Success and Result then
	local Object = Result.Instance
	if Object == Target then
		print("Success!")
	end
else
	warn(Success, Result)
end

Hope you find this useful :slight_smile:
If you notice any errors or improvements to be made, let me know.

7 Likes

This will return True and information about what it hit if the raycast succeeds and it will return False and an error message if the raycast fails

if a raycast doesn’t hit something, it won’t throw an error, but only return nil

you should do this instead:


local Origin = workspace:WaitForChild("Origin")
local Obstacle = workspace:WaitForChild("Obstacle")
local Target = workspace:WaitForChild("Target") -- Testing purposes

local Parameters = RaycastParams.new()
Parameters.FilterType = Enum.RaycastFilterType.Exclude
Parameters:AddToFilter({Obstacle}) -- Add anything to avoid

local Direction = Vector3.new(0,0,100)

local Result =  workspace:Raycast(Origin.Position, Direction, Parameters)

if not Result then
     print(" i did not hit anything")
     return
end

local Object = Result.Instance
if Object == Target then
	print("Success!")
end

5 Likes