Want a special badge and 30 bux? Make some PolyWorld! Content and post it in the Creations forum!
Dark mode is incomplete! Please report any issues to the Discord.

RaycastModule

LocalScripts Scripts Not creatable

RaycastModule casts rays through the world and converts between screen and world space.
Available as the global RaycastModule.


Overview

Methods


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.

FieldTypeNotes
FilterTypestring"Exclude" or "Include"
FilterDescendantsInstancesList of InstanceInstances (and descendants) to skip or allow
IgnoreTransparentPartsboolWhen true, parts with Transparency greater than 0 are ignored
CollisionGroupstring?Optional collision group name

RaycastResult

Returned by Raycast on a hit.

FieldTypeNotes
InstanceInstanceUsually a Part
PositionVector3World hit position
NormalVector3Surface normal at the hit
DistancenumberDistance from origin to the hit