---
name: Ray
last_updated: 2026-12-04T23:21:36Z
type: datatype
summary: "Represents a line with a starting point that casts infinitely in a specific direction."
---

# Ray

Represents a line with a starting point that casts infinitely in a specific
direction.

**Type:** datatype

## Description

The [Ray](/docs/reference/engine/datatypes/Ray.md) 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

### Ray.new

**Signature:** `Ray.new(Origin: Vector3, Direction: Vector3)`

Returns a new [Ray](/docs/reference/engine/datatypes/Ray.md) 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. |

**Ray.new()**

Shows how to create a [Ray](/docs/reference/engine/datatypes/Ray.md) from an origin and a direction that need
not have length 1.

```lua
local ray = Ray.new(Vector3.new(0, 5, 0), Vector3.new(0, -10, 0))
print(typeof(ray)) --> Ray
print(ray.Direction.Magnitude) --> 10
```

## Properties

### Ray.Unit

**Type:** `Ray`

The [Ray](/docs/reference/engine/datatypes/Ray.md) with a normalized direction (the direction has a
magnitude of `1`).

**Ray.Unit**

Shows how to get a copy of a [Ray](/docs/reference/engine/datatypes/Ray.md) whose direction has length 1.

```lua
local ray = Ray.new(Vector3.zero, Vector3.new(10, 0, 0))
print(ray.Unit.Direction) --> 1, 0, 0
print(ray.Unit.Direction.Magnitude) --> 1
```

### Ray.Origin

**Type:** `Vector3`

The position of the origin.

**Ray.Origin**

Shows how to read where a [Ray](/docs/reference/engine/datatypes/Ray.md) starts.

```lua
local ray = Ray.new(Vector3.new(1, 2, 3), Vector3.new(0, 0, 10))
print(ray.Origin) --> 1, 2, 3
```

### Ray.Direction

**Type:** `Vector3`

The direction vector of the [Ray](/docs/reference/engine/datatypes/Ray.md).

**Ray.Direction**

Shows how to read which way a [Ray](/docs/reference/engine/datatypes/Ray.md) points, which keeps the length it
was created with.

```lua
local ray = Ray.new(Vector3.new(1, 2, 3), Vector3.new(0, 0, 10))
print(ray.Direction) --> 0, 0, 10
```

## Methods

### Ray:ClosestPoint

**Signature:** `Ray:ClosestPoint(point: Vector3): Vector3`

Returns a [Vector3](/docs/reference/engine/datatypes/Vector3.md) projected onto the ray so that it is within
the [Ray](/docs/reference/engine/datatypes/Ray.md) line of sight.

**Note:** the [Ray](/docs/reference/engine/datatypes/Ray.md) **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:** `Vector3` — The point on the ray closest to the given point, or the ray origin if
the projection falls behind it.

**Ray:ClosestPoint()**

Shows how to find the point on a [Ray](/docs/reference/engine/datatypes/Ray.md) nearest to another point,
including a point behind its origin.

```lua
local ray = Ray.new(Vector3.zero, Vector3.new(10, 0, 0))

print(ray:ClosestPoint(Vector3.new(4, 3, 0))) --> 4, 0, 0

-- A ray has a start, so a point behind it snaps to the origin.
print(ray:ClosestPoint(Vector3.new(-5, 2, 0))) --> 0, 0, 0
```

### Ray:Distance

**Signature:** `Ray:Distance(point: Vector3): number`

Returns the distance between the given point and the point on the ray
nearest to the given point ([Ray.ClosestPoint(point)](/docs/reference/engine/datatypes/Ray.md)).

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `point` | `Vector3` |  | The point in 3D space to measure distance from. |

**Returns:** `number` — The shortest distance between the given point and the closest point on
the ray.

**Ray:Distance()**

Shows how to measure how far a point is from the nearest point on a
[Ray](/docs/reference/engine/datatypes/Ray.md).

```lua
local ray = Ray.new(Vector3.zero, Vector3.new(10, 0, 0))

print(ray:Distance(Vector3.new(4, 3, 0))) --> 3
print(ray:Distance(Vector3.new(4, 0, 0))) --> 0
```