Skip to main content

Raycast

A global API which provides functions for raycasting

warning

This page is a WIP. It contains all the information in Figura's documentation but we're working on adding more helpful descriptions.


aabb()

Raycasts based on a start position, an end position, and an array of Axis Aligned Bounding Boxes defined by the player.

AABBs are encoded as a table with indicies 1 and 2 being a Vector3.

{vec(0,0,0),vec(1,0.5,1)} is a valid AABB, with { {vec(0,0,0),vec(1,0.5,1)}, {vec(0,0.5,0.5),vec(1,1,1)} } being a valid AABB array.

This function returns the AABB table that was hit, the exact position hit as a Vector3, the side of the AABB hit as a string or nil if inside an AABB, and the index of the AABB that was hit in the array

aabb(startpos, endpos, aabbs)

Parameters:

NameTypeDescriptionDefault
startVector3--
endVector3--
aabbsTable--

Returns:

TypeDescription
TableHit AABB
Vector3Hit pos
StringHit side or nil if inside the AABB
IntegerInteger of hit AABB

Example:

local eyePos = player:getPos() + vec(0, player:getEyeHeight(), 0)
local eyeEnd = eyePos + (player:getLookDir() * 20)
local hitLocation = { { vec(0, 0, 0), vec(1, 1, 1) } } -- this is the block location of 0,0,0 in the world
local aabb, hitPos, side, aabbHitIndex = raycast:aabb(eyePos, eyeEnd, hitLocation)

block()

Raycasts a Block in the world.

If successful, returns the BlockState hit, the exact world position hit as a Vector3, and the side of the block that was hit.

When unsuccessful, returns nil.

blockCastType and fluidCastType determine how the raycast handles block shapes and fluids.

Will default to "COLLIDER" and "NONE" when nil

block(startpos, endpos, blockCastType, fluidCastType)

Parameters:

NameTypeDescriptionDefault
startVector3--
endVector3--
blockCastTypeString--
fluidCastTypeString--

Returns:

TypeDescription
BlockStateHit block
Vector3Hit position
StringHit block face

Example:

local eyePos = player:getPos() + vec(0, player:getEyeHeight(), 0)
local eyeEnd = eyePos + (player:getLookDir() * 20)
local block, hitPos, side = raycast:block(eyePos, eyeEnd)

entity()

Raycasts an Entity in the world

If successful, returns the EntityAPI hit and the exact world position hit as a Vector3.

When unsuccessful, returns nil.

predicate is a function that prevents specific entities from being raycasted.

Takes in a single EntityAPI object. Return true for valid entities, false for invalid.

Marks all entities as valid when nil

entity(startpos, endpos, predicate)

Parameters:

NameTypeDescriptionDefault
startVector3--
endVector3--
predicateFunction--

Returns:

TypeDescription
EntityHit entity
Vector3Hit position

Example:

local eyePos = player:getPos() + vec(0, player:getEyeHeight(), 0)
local eyeEnd = eyePos + (player:getLookDir() * 20)
local entity, hitPos = raycast:entity(eyePos, eyeEnd,function(x) -- x is the entity hit by the raycast
if x == player then -- if the entity hit is the player
return false -- don't include the player in the results
else
return true
end
end)