World:raycast

Edit

Traces a ray through the world and calls a function for each collider that was hit.

The callback can be left off, in which case the closest hit will be returned.





Arguments

NameTypeDefaultDescription
x1number The x coordinate of the origin of the ray.
y1number The y coordinate of the origin of the ray.
z1number The z coordinate of the origin of the ray.
x2number The x coordinate of the endpoint of the ray.
y2number The y coordinate of the endpoint of the ray.
z2number The z coordinate of the endpoint of the ray.
filterstringnil An optional tag filter. Pass one or more tags separated by spaces to only return colliders with those tags. Or, put ~ in front the tags to exclude colliders with those tags.
callbackfunction The function to call when an intersection is detected (see notes).

Returns

Nothing

Arguments

NameTypeDefaultDescription
originVec3 The origin of the ray.
endpointVec3 The endpoint of the ray.
filterstringnil An optional tag filter. Pass one or more tags separated by spaces to only return colliders with those tags. Or, put ~ in front the tags to exclude colliders with those tags.
callbackfunction The function to call when an intersection is detected (see notes).

Returns

Nothing

Arguments

NameTypeDefaultDescription
x1number The x coordinate of the origin of the ray.
y1number The y coordinate of the origin of the ray.
z1number The z coordinate of the origin of the ray.
x2number The x coordinate of the endpoint of the ray.
y2number The y coordinate of the endpoint of the ray.
z2number The z coordinate of the endpoint of the ray.
filterstringnil An optional tag filter. Pass one or more tags separated by spaces to only return colliders with those tags. Or, put ~ in front the tags to exclude colliders with those tags.

Returns

NameTypeDescription
colliderCollider The Collider that was hit.
shapeShape The Shape that was hit.
xnumber The x coordinate of the impact point, in world space.
ynumber The y coordinate of the impact point, in world space.
znumber The z coordinate of the impact point, in world space.
nxnumber The x component of the normal vector.
nynumber The y component of the normal vector.
nznumber The z component of the normal vector.
trianglenumber The index of the triangle that was hit, or nil if a MeshShape was not hit.

Arguments

NameTypeDefaultDescription
originVec3 The origin of the ray.
endpointVec3 The endpoint of the ray.
filterstringnil An optional tag filter. Pass one or more tags separated by spaces to only return colliders with those tags. Or, put ~ in front the tags to exclude colliders with those tags.

Returns

NameTypeDescription
colliderCollider The Collider that was hit.
shapeShape The Shape that was hit.
xnumber The x coordinate of the impact point, in world space.
ynumber The y coordinate of the impact point, in world space.
znumber The z coordinate of the impact point, in world space.
nxnumber The x component of the normal vector.
nynumber The y component of the normal vector.
nznumber The z component of the normal vector.
trianglenumber The index of the triangle that was hit, or nil if a MeshShape was not hit.

Notes

The callback function is passed a collider, a shape, a world-space point, a world-space normal, and a fraction:

function(collider, shape, x, y, z, nx, ny, nz, fraction)
  return fraction
end

The callback can return a fraction value used to limit the range of further hits. For example:

Raycasts will hit sensors and sleeping colliders, but will not hit disabled colliders.

See also