At its most basic level, raycasting is the act of sending out an invisible ray from a Vector3 point in a specific direction with a defined length. Once cast, you can detect if the ray hits a BasePart or Terrain cell.
You can cast a ray with the WorldRoot:Raycast() method (Workspace:Raycast()) from a Vector3 origin in a Vector3 direction.
Basic Raycastlocal Workspace = game:GetService("Workspace")local rayOrigin = Vector3.new(0, 0, 0)local rayDirection = Vector3.new(0, -100, 0)local raycastResult = Workspace:Raycast(rayOrigin, rayDirection)
When applicable, you can calculate an unknown directional vector (rayDirection) using a known origin and destination. This is useful when casting a ray between two points that can change, such as from one player character to another.
The origin plus a directional vector indicate the ray's destination:
Subtract from both sides of the equation:
The ray's direction equals the destination minus the origin:
local Workspace = game:GetService("Workspace")local rayOrigin = Workspace.TestOrigin.Positionlocal rayDestination = Workspace.TestDestination.Positionlocal rayDirection = rayDestination - rayOriginlocal raycastResult = Workspace:Raycast(rayOrigin, rayDirection)
Filter options
WorldRoot:Raycast() accepts an optional RaycastParams object which tells the raycast to selectively include or exclude certain BaseParts, ignore the Water material for Terrain, or use a collision group.
| Key | Description |
|---|---|
| FilterDescendantsInstances | Array of objects whose descendants are used in filtering raycasting candidates. |
| FilterType | Enum.RaycastFilterType enum which determines how the FilterDescendantsInstances array is used in the raycast operation. |
| IgnoreWater | Boolean which determines whether the Water material is considered when raycasting against Terrain. |
| CollisionGroup | String name of the collision group used for the raycasting operation. |
Raycast Filteringlocal Workspace = game:GetService("Workspace")local rayOrigin = Vector3.zerolocal rayDirection = Vector3.new(0, -100, 0)local raycastParams = RaycastParams.new()raycastParams.FilterDescendantsInstances = {script.Parent}raycastParams.FilterType = Enum.RaycastFilterType.ExcluderaycastParams.IgnoreWater = truelocal raycastResult = Workspace:Raycast(rayOrigin, rayDirection, raycastParams)
Hit detection
If the raycasting operation hits an eligible BasePart or Terrain cell, a RaycastResult object is returned containing the results. To test for a hit, confirm that the result is not nil and utilize the following properties as needed.
| Property | Description |
|---|---|
| Instance | The BasePart or Terrain cell that the ray intersected. |
| Position | Vector3 position of the intersection between the ray and the Instance. |
| Distance | Distance between the ray origin and intersection point. |
| Material | The Enum.Material of the BasePart or Terrain at the intersection point. |
| Normal | Vector3 of the normal vector of the intersected face. |
Raycast Hit Detectionlocal Workspace = game:GetService("Workspace")local rayOrigin = Vector3.zerolocal rayDirection = Vector3.new(0, -100, 0)local raycastResult = Workspace:Raycast(rayOrigin, rayDirection)if raycastResult thenprint("Instance:", raycastResult.Instance)print("Position:", raycastResult.Position)print("Distance:", raycastResult.Distance)print("Material:", raycastResult.Material)print("Normal:", raycastResult.Normal)elsewarn("No raycast result!")end