2 min read

Source: Roblox Creator Hub · CC BY 4.0 · View source · Code samples: MIT Imported 2026-10-03. Formatting adapted for this site.

Ray

The Ray data type represents a half-line, finite in one direction but infinite in the other. It can be defined by a 3D point, where the line originates from, and a direction vector, which is the direction it goes in.

Constructors

NameType / ReturnsDescription
Ray.newReturns a Ray with the given Origin and Direction.

Ray.new

Returns a new Ray with given Origin and Direction.

Parameters

NameTypeDefaultDescription
OriginVector3The starting point of the ray in 3D space.
DirectionVector3The direction vector of the ray. Does not need to be a unit vector.

Properties

NameType / ReturnsDescription
Ray.UnitRayThe Ray with a normalized direction (the direction has a magnitude of 1).
Ray.OriginVector3The position of the origin.
Ray.DirectionVector3The direction vector of the Ray.

Ray.Unit

The Ray with a normalized direction (the direction has a magnitude of 1).

FieldValue
typeRay

Ray.Origin

The position of the origin.

FieldValue
typeVector3

Ray.Direction

The direction vector of the Ray.

FieldValue
typeVector3

Methods

NameType / ReturnsDescription
Ray:ClosestPointVector3Returns a Vector3 projected onto the ray so that it is within the Ray line of sight.
Ray:DistancenumberReturns the distance between the given point and the closest point on the Ray.

Ray:ClosestPoint

Returns a Vector3 projected onto the ray so that it is within the Ray line of sight.

Note: the Ray must be a unit ray for this method to behave as expected!

Parameters

NameTypeDefaultDescription
pointVector3The point in 3D space to project onto the ray.

Returns

TypeDescription
Vector3The point on the ray closest to the given point, or the ray origin if the projection falls behind it.

Ray:Distance

Returns the distance between the given point and the point on the ray nearest to the given point (Ray.ClosestPoint(point)).

Parameters

NameTypeDefaultDescription
pointVector3The point in 3D space to measure distance from.

Returns

TypeDescription
numberThe shortest distance between the given point and the closest point on the ray.