RaycastModule casts rays through the world and converts between screen and world space.
Available as the global RaycastModule.
Overview
Methods
- :MakeRaycastParams() : RaycastParams
- :Raycast(origin: Vector3, direction: Vector3, params: RaycastParams?) : RaycastResult?
- :ScreenToWorld(screenX: number, screenY: number, depth: number?) : (origin: Vector3, direction: Vector3)
- :PointToScreen(worldPoint: Vector3) : (x: number, y: number, onScreen: bool)
API
Methods
MakeRaycastParams
MakeRaycastParams() : RaycastParams
Creates a new filter table for Raycast.
local params = RaycastModule:MakeRaycastParams()
params.FilterType = "Exclude"
params.FilterDescendantsInstances = { workspace.Lobby }
params.IgnoreTransparentParts = true
Raycast
Raycast(origin: Vector3, direction: Vector3, params: RaycastParams?) : RaycastResult?
Casts a ray from origin along direction.
The length of direction is the max distance.
Returns a RaycastResult on hit, or nil if nothing was hit.
local result = RaycastModule:Raycast(
Vector3.new(0, 10, 0),
Vector3.new(0, -50, 0)
)
if result then
print(result.Instance.Name, result.Position)
end
ScreenToWorld
ScreenToWorld(screenX: number, screenY: number, depth: number?) : origin, direction
LocalScripts only.
Builds a world-space ray from a screen pixel, like tool aiming / projection.
depth is optional far distance used to build direction (engine default if omitted).
Returns origin and direction suitable for Raycast.
local origin, direction = RaycastModule:ScreenToWorld(
InputModule.MousePosition.X,
InputModule.MousePosition.Y
)
local hit = RaycastModule:Raycast(origin, direction)
PointToScreen
PointToScreen(worldPoint: Vector3) : x, y, onScreen
LocalScripts only.
Projects a world point to screen pixels.
onScreen is false when the point is behind the camera or outside the view.
local x, y, onScreen = RaycastModule:PointToScreen(part.Position)
if onScreen then
-- draw or place UI at x, y
end
RaycastParams
Created by MakeRaycastParams.
| Field | Type | Notes |
|---|---|---|
FilterType | string | "Exclude" or "Include" |
FilterDescendantsInstances | List of Instance | Instances (and descendants) to skip or allow |
IgnoreTransparentParts | bool | When true, parts with Transparency greater than 0 are ignored |
CollisionGroup | string? | Optional collision group name |
RaycastResult
Returned by Raycast on a hit.
