Raycasting
Summary
Raycasting in games is a technique that allows you to determine what objects are in the way of a line of sight. This is useful for a variety of things, such as determining what the player is looking at, or what objects are in the way of a projectile.
Creating a Raycast
Creating a raycst isn't that hard in all honesty. workspace:Raycast(orgin, distance, raycastPrams?)
takes 3 arguments.
Argument | Description |
---|---|
Origin | The position of the raycast |
Direction | The direction of the raycast |
RaycastPrams | A table of parameters for the raycast |
local part = workspace:WaitForChild('Raycaster')
local orgin = part.Position
local direction = part.CFrame.lookVector * 100
local raycastResult = workspace:Raycast(orgin, direction)
print(raycastResult)
If you dont know how direction parameter works:
part.CFrame.lookVector * 100
is getting the Vector3 of wheres the part is looking at, then multiplying it by 100 to move that lookVector by 100 studs. If you want the raycast go to a spicfic end point, you can do endPoint - orgin
to get the direction to the end point Vector3.
This code here is basically defining the origin start position
, direction direction of the beam
, and using those to raycast the beam. The result of the raycast is stored in raycastResult
. If you print it, you will get a table of information about the raycast.
Property | Description |
---|---|
Instance | The instance that was hit |
Position | The position of the raycast |
Normal | The normal of the surface that was hit |
Material | The material of the surface that was hit |
Distance | The distance from the orgin to the hit |
If nothing hits the raycast, the workspace:Raycast(...)
will return nil. Make sure to check if the raycast result is not nil before using it, and or you can do workspace:Raycast(...) or {}
to prevent a attempt to index nil with 'Instance'
error.
Using RaycastParams
RaycastParams is a table that allows you to filter out what you want to hit with the raycast. For example, if you want to raycast only to hit parts that you want to be hit, you can do:
local prams = RaycastParams.new()
prams.FilterType = Enum.RaycastFilterType.Whitelist -- You can do Blacklist to do the opposite
prams.FilterDescendantsInstances = { workspace:WaitForChild('Parts') }
This here is using an Enum.RaycastFilterType
of Whitelist
to only hit the parts that are in the Parts
folder/model. You can also use Blacklist
to hit everything except the parts in the Parts
folder/model. Other properties in RaycastParams are:
Property | Description |
---|---|
FilterType | The type of filter to use. Can be Whitelist or Blacklist |
FilterDescendantsInstances | A table of instances to filter out |
IgnoreWater | Whether or not to ignore water |
CollisionGroup | The collision group to use |
A lot of people make this mistake, but FilterDescendantsInstances must be an array of baseparts! Cannot be prams.FilterDescendantsInstances = workspace:WaitForChild('Parts')
, but prams.FilterDescendantsInstances = { workspace:WaitForChild('Parts') }
local part = workspace:WaitForChild('Raycaster')
local orgin = part.Position
local direction = part.CFrame.lookVector * 100
local prams = RaycastParams.new()
prams.FilterType = Enum.RaycastFilterType.Blacklist
prams.FilterDescendantsInstances = { workspace:WaitForChild('Glass') }
prams.IgnoreWater = true
local raycastResult = workspace:Raycast(orgin, direction, prams)
print(raycastResult)
Stiching together the raycast with prams, we can create a "laser" beam that goes through glass, and water, but will stop at parts/baseparts.