Raycasting⚓︎
Roblox engine allows its users to cast an invisible ray from a Vector3 position towards a specified direction. Unlike normal rays, Roblox enables you to set the length of ray according to your needs. Using raycast, you can detect if the ray hits a Basepart or Terrain.
Casting A Ray⚓︎
For casting a ray we use the method :Raycast()
of worldroot (workspace). Raycast()
takes three arguments and returns RaycastResult
- Origin type: Vector3
- Direction type: Vector3
- RaycastParams type: RaycastParams
Origin⚓︎
Origin of a ray is basically a Vector3 position of the world, from where the ray will start. We will take two parts. A red part and a green part and cast ray between them.
We have selected position of red part as origin for the ray.Direction⚓︎
Direction is the Vector3 with a defined magnitude. Direction of a ray from a known destination (GreenPart.Position) can be easily calculated by using following formula
- Destination - Origin = Direction
If you are familiar with vectors then we are basically getting a Vector3 between the two positions. Once we have calculated the directional vector, we will adjust the length of it. For doing so we will get the unit vector of directional vector and multiply it with the number of studs (the length of ray in studs).
In above example, the magnitude of ray is set to 100 studs.
RaycastParams⚓︎
RaycastParams carries the parameters of Raycast()
. It's property, FilterDescendantsInstances
stores a table. It can be either a Exclude
or Include
depending on FilterType
.
Note
Enum.RaycastFilterType.Include
EveryBasePart
other than those given in filter list and their descendants will be ignored.Enum.RaycastFilterType.Exclude
EveryBasePart
other than those given in filter list and their descendants will be considered.
local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = {RedPart}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
In these params we have excluded the red part in order to prevent ray from being incident on the walls of red part.
Our overall code will be:
local origin = RedPart.Position
local direction = (GreenPart.Position - RedPart.Position).Unit * 100
local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = {RedPart}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local Raycast_result = workspace:Raycast(origin, direction, raycastparams)
RaycastResult⚓︎
If the ray hits a basepart, it will return RaycastResult. It's properties carries result of raycast
- RaycastResult.Instance | The BasePart intersected by the ray.
- RaycastResult.Position | The world position where intersection took place.
- RaycastResult.Material | The material of BasePart intersected by the ray.
- RaycastResult.Normal | The vector perpendicular to the face of intersected surface.
local origin = RedPart.Position
local direction = (GreenPart.Position - RedPart.Position).Unit * 100
local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = {RedPart}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local Raycast_result = workspace:Raycast(origin, direction, raycastparams)
print(Raycast_result.Position)
Warning
Raycast_result
will be nil
if ray didn't hit any object.
Now if we place another part between the two parts then ray will intersect the middle one and Raycast_result
will carry properties related to the yellow part.
Closing!⚓︎
As always, we hope you enjoyed reading and can utilize raycasting according to your needs. Whatever you are learning, please practice it on the spot. Just reading will not help you if you aren't practicing them. In case of any mistakes, typos, etc please report the article!