Ray

A ray has one origin point in the 3D scene and a direction. It returns potentials Impacts and the distance from the origin when that happens.

Constructors

Ray ( Number3 origin, Number3 direction )

Creates a Ray with an origin and a direction.

local ray = Ray(Camera.Position, Camera.Forward)
local impact = ray:Cast()

Functions

Impact Cast ( CollisionGroups filterIn optional, Object filterOut optional, boolean onlyFirstImpact optional )
Impact Cast ( Shape filterIn optional, Object filterOut optional, boolean onlyFirstImpact optional )

Casts a ray and returns an Impact that contains information about what's been hit. If nothing was it, returns nil instead.

The ray will intersect with any objects matching the filterIn collision groups. If nil is provided, the ray intersects with all CollisionGroups by default.
Alternatively, the filterIn parameter can be a Shape, in which case the raycast will only be performed against the shape's blocks.

Additionally, an object can be provided as filterOut to exclude it as a potential result. However, it is generally a best practice to filter using collision groups.

The third parameter onlyFirstImpact dictates whether or not to receive only the first impacted object, or all of the intersected objects in a table. It is set to true by default.

Pointer.Down = function( pointerEvent )
    local ray = Ray(pointerEvent.Position, pointerEvent.Direction)
    local impact = ray:Cast()
    if impact.Block ~= nil then
      print("block hit:", impact.Block)
    end
end

-- cast rays from Camera's position to remove cubes in the map
Client.Action2 = function()
    local ray = Ray(Camera.Position, Camera.Forward)
    local impact = ray:Cast(Map.CollisionGroups) -- only consider the map for collisions
    if impact.Block ~= nil then
      impact.Block:Remove()
    end
end

-- cast ray down from Player's position to see
-- if there's something under it:
Client.Action3 = function()
    local ray = Ray(Player.Position, {0, -1, 0})
    local impact = ray:Cast(nil, Player) -- filter out Player to avoid direct impacts with it
    if impact ~= nil then
        print("found something under the player, distance:", impact.Distance)
    end
end
-- When a single Shape is provided
-- the Shape's internal block touched is provided
-- as part of the Impact object
Client.Action2 = function()
    local ray = Ray(Camera.Position, Camera.Forward)
    local impact = ray:Cast(myShape) -- only consider a Shape for collisions
    -- impact.Block is Number3(0, 5, 12)
    -- impact.Distance is 42,72234
    -- impact.Object is the `myShape` Shape
end

Properties

The direction of the ray, in world coordinate system.

Defines items colliding with the ray. nil by default, meaning the ray collides with everything.

nil by default. Can be set to an Object to filter it out from possible collisions.

The origin of the ray, in world coordinate system.