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
| Name | Type / Returns | Description |
|---|---|---|
| Ray.new | Returns a Ray with the given Origin and Direction. |
Ray.new
Returns a new Ray with given Origin and Direction.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| Origin | Vector3 | The starting point of the ray in 3D space. | |
| Direction | Vector3 | The direction vector of the ray. Does not need to be a unit vector. |
Properties
| Name | Type / Returns | Description |
|---|---|---|
| Ray.Unit | Ray | The Ray with a normalized direction (the direction has a magnitude of 1). |
| Ray.Origin | Vector3 | The position of the origin. |
| Ray.Direction | Vector3 | The direction vector of the Ray. |
Ray.Unit
The Ray with a normalized direction (the direction has a magnitude of 1).
| Field | Value |
|---|---|
| type | Ray |
Ray.Origin
The position of the origin.
| Field | Value |
|---|---|
| type | Vector3 |
Ray.Direction
The direction vector of the Ray.
| Field | Value |
|---|---|
| type | Vector3 |
Methods
| Name | Type / Returns | Description |
|---|---|---|
| Ray:ClosestPoint | Vector3 | Returns a Vector3 projected onto the ray so that it is within the Ray line of sight. |
| Ray:Distance | number | Returns 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
| Name | Type | Default | Description |
|---|---|---|---|
| point | Vector3 | The point in 3D space to project onto the ray. |
Returns
| Type | Description |
|---|---|
| Vector3 | The 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
| Name | Type | Default | Description |
|---|---|---|---|
| point | Vector3 | The point in 3D space to measure distance from. |
Returns
| Type | Description |
|---|---|
| number | The shortest distance between the given point and the closest point on the ray. |