Scripting / Workspace method
workspace:Raycast()
Casts a ray through the physics world and returns the first hit.
Syntax
workspace:Raycast(origin, direction)Parameters
| Name | Type | Description |
|---|---|---|
origin | Vector3 | World-space ray start position. |
direction | Vector3 | Direction and maximum distance. Its magnitude becomes the cast length. |
Return value
| Name | Type | Description |
|---|---|---|
Position | Vector3 | World-space hit point. |
Normal | Vector3 | Surface normal at the hit. |
Distance | number | Distance from origin to the hit. |
Instance | Instance? | Hit Glorpia instance when the collider maps to one. |
Returns
nil when the direction is effectively zero or the ray does not hit anything.Example: Cast Downward
Luau
local origin = Vector3.new(0, 20, 0)
local direction = Vector3.new(0, -50, 0)
local hit = workspace:Raycast(origin, direction)
if hit then
print("Hit", hit.Instance, "at", hit.Position)
print("Distance", hit.Distance)
end