---
name: Vector2
last_updated: 2026-12-04T23:21:36Z
type: datatype
summary: "Represents a 2D value with direction and magnitude."
---

# Vector2

Represents a 2D value with direction and magnitude.

**Type:** datatype

## Description

The [Vector2](/docs/reference/engine/datatypes/Vector2.md) data type represents a 2D value with direction and
magnitude. Some applications include GUI elements and 2D mouse positions.

#### Math Operations

The following math operations are valid for the [Vector2](/docs/reference/engine/datatypes/Vector2.md) data type:

| Operation | Description |
| --- | --- |
| [Vector2](/docs/reference/engine/datatypes/Vector2.md) `+` [Vector2](/docs/reference/engine/datatypes/Vector2.md) | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component of the second added to the corresponding component of the first. |
| [Vector2](/docs/reference/engine/datatypes/Vector2.md) `-` [Vector2](/docs/reference/engine/datatypes/Vector2.md) | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component of the second subtracted from the corresponding component of the first. |
| [Vector2](/docs/reference/engine/datatypes/Vector2.md) `*` [Vector2](/docs/reference/engine/datatypes/Vector2.md) | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component of the second multiplied by the corresponding component of the first. |
| [Vector2](/docs/reference/engine/datatypes/Vector2.md) `/` [Vector2](/docs/reference/engine/datatypes/Vector2.md) | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component of the first divided by the corresponding component of the second. |
| [Vector2](/docs/reference/engine/datatypes/Vector2.md) `*` `number` | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component multiplied by the number. |
| [Vector2](/docs/reference/engine/datatypes/Vector2.md) `/` `number` | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component divided by the number. |

## Constructors

### Vector2.new

**Signature:** `Vector2.new(x: number, y: number)`

Returns a [Vector2](/docs/reference/engine/datatypes/Vector2.md) from the given x and y components. Both
parameters default to `0` if not provided.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `x` | `number` |  | The x-coordinate of the new vector (defaults to 0). |
| `y` | `number` |  | The y-coordinate of the new vector (defaults to 0). |

**Vector2.new()**

Shows how to create a [Vector2](/docs/reference/engine/datatypes/Vector2.md) from its x and y components.

```lua
local point = Vector2.new(3, 4)
print(point) --> 3, 4

-- Omitted components default to 0.
print(Vector2.new()) --> 0, 0
print(Vector2.new(5)) --> 5, 0
```

## Constants

| Name | Type | Description |
|------|------|-------------|
| `Vector2.zero` | `Vector2` | A [Vector2](/docs/reference/engine/datatypes/Vector2.md) with a magnitude of zero. |
| `Vector2.one` | `Vector2` | A [Vector2](/docs/reference/engine/datatypes/Vector2.md) with a value of 1 on every axis. |
| `Vector2.xAxis` | `Vector2` | A [Vector2](/docs/reference/engine/datatypes/Vector2.md) with a value of 1 on the X axis. |
| `Vector2.yAxis` | `Vector2` | A [Vector2](/docs/reference/engine/datatypes/Vector2.md) with a value of 1 on the Y axis. |

## Properties

### Vector2.X

**Type:** `number`

The x-coordinate of the [Vector2](/docs/reference/engine/datatypes/Vector2.md), corresponding to the first
argument passed to [Vector2.new()](/docs/reference/engine/datatypes/Vector2.md). This property is read-only.

**Vector2.X**

Shows how to read the X component of a [Vector2](/docs/reference/engine/datatypes/Vector2.md), which cannot be
changed in place.

```lua
local position = Vector2.new(3, 4)
print(position.X) --> 3

-- A vector cannot be changed in place, so build a new one to change X.
print(Vector2.new(position.X + 1, position.Y)) --> 4, 4
```

### Vector2.Y

**Type:** `number`

The y-coordinate of the [Vector2](/docs/reference/engine/datatypes/Vector2.md), corresponding to the second
argument passed to [Vector2.new()](/docs/reference/engine/datatypes/Vector2.md). This property is read-only.

**Vector2.Y**

Shows how to read the Y component of a [Vector2](/docs/reference/engine/datatypes/Vector2.md), which cannot be
changed in place.

```lua
local position = Vector2.new(3, 4)
print(position.Y) --> 4

-- A vector cannot be changed in place, so build a new one to change Y.
print(Vector2.new(position.X, position.Y + 1)) --> 3, 5
```

### Vector2.Magnitude

**Type:** `number`

The length (magnitude) of the [Vector2](/docs/reference/engine/datatypes/Vector2.md), computed as
`math.sqrt(X^2 + Y^2)`. This property is read-only.

**Vector2.Magnitude**

Shows how to measure the length of a [Vector2](/docs/reference/engine/datatypes/Vector2.md) and the distance
between two points.

```lua
print(Vector2.new(3, 4).Magnitude) --> 5
print(Vector2.zero.Magnitude) --> 0

-- The magnitude of the difference between two points is the distance between them.
local a = Vector2.new(1, 1)
local b = Vector2.new(4, 5)
print((b - a).Magnitude) --> 5
```

### Vector2.Unit

**Type:** `Vector2`

A normalized copy of the [Vector2](/docs/reference/engine/datatypes/Vector2.md) — one that has the same
direction as the original but a magnitude of 1. If the vector has zero
magnitude the result is undefined (contains `nan` values). This property
is read-only.

**Vector2.Unit**

Shows how to get a direction of length 1 from a [Vector2](/docs/reference/engine/datatypes/Vector2.md) and scale
it to a new length.

```lua
local velocity = Vector2.new(0, 4)
print(velocity.Unit) --> 0, 1
print(velocity.Unit.Magnitude) --> 1

-- Multiply the direction by a number to move at a chosen speed.
print(velocity.Unit * 10) --> 0, 10
```

## Methods

### Vector2:Cross

**Signature:** `Vector2:Cross(other: Vector2): number`

Returns the 2D cross product of this vector with `other`, computed as
`self.X * other.Y - self.Y * other.X`. Unlike [Vector3:Cross()](/docs/reference/engine/datatypes/Vector3.md)
which returns a vector, the 2D cross product returns a scalar representing
the signed magnitude of the perpendicular (Z) component. The result is
positive when `other` is counter-clockwise from `self`, negative when
clockwise, and zero when the vectors are parallel.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `other` | `Vector2` |  | The vector to compute the cross product against. |

**Returns:** `number` — The signed scalar magnitude of the perpendicular component, computed
as `self.X * other.Y - self.Y * other.X`.

**Vector2:Cross()**

Shows how the 2D cross product of two [Vector2](/docs/reference/engine/datatypes/Vector2.md) values is a number
whose sign tells which side one vector is on.

```lua
local right = Vector2.xAxis
local up = Vector2.yAxis

print(right:Cross(up)) --> 1
print(up:Cross(right)) --> -1

-- The cross product is 0 when the vectors are parallel.
print(right:Cross(Vector2.new(2, 0))) --> 0
```

### Vector2:Abs

**Signature:** `Vector2:Abs(): Vector2`

Returns a new vector from the absolute values of the original's
components. For example, a vector of `(-2, 4)` returns a vector of
`(2, 4)`.

**Returns:** `Vector2` — A new vector whose components are the absolute values of the
original's components.

**Vector2:Abs()**

Shows how to take the absolute value of each component of a
[Vector2](/docs/reference/engine/datatypes/Vector2.md).

```lua
local v = Vector2.new(-1.5, 2.25)
print(v:Abs()) --> 1.5, 2.25
```

### Vector2:Ceil

**Signature:** `Vector2:Ceil(): Vector2`

Returns a new vector from the ceiling of the original's components. For
example, a vector of `(-2.6, 5.1)` returns a vector of `(-2, 6)`.

**Returns:** `Vector2` — A new vector whose components are the ceiling (rounded up) of the
original's components.

**Vector2:Ceil()**

Shows how to round each component of a [Vector2](/docs/reference/engine/datatypes/Vector2.md) up to the next whole
number.

```lua
local v = Vector2.new(-1.5, 2.25)
print(v:Ceil()) --> -1, 3
```

### Vector2:Floor

**Signature:** `Vector2:Floor(): Vector2`

Returns a new vector from the floor of the original's components. For
example, a vector of `(-2.6, 5.1)` returns a vector of `(-3, 5)`.

**Returns:** `Vector2` — A new vector whose components are the floor (rounded down) of the
original's components.

**Vector2:Floor()**

Shows how to round each component of a [Vector2](/docs/reference/engine/datatypes/Vector2.md) down to the previous
whole number.

```lua
local v = Vector2.new(-1.5, 2.25)
print(v:Floor()) --> -2, 2
```

### Vector2:Sign

**Signature:** `Vector2:Sign(): Vector2`

Returns a new vector from the sign (-1, 0, or 1) of the original's
components. For example, a vector of `(-2.6, 5.1)` returns a vector of
`(-1, 1)`.

**Returns:** `Vector2` — A new vector whose components are the sign (-1, 0, or 1) of the
original's components.

**Vector2:Sign()**

Shows how to get the sign of each component of a [Vector2](/docs/reference/engine/datatypes/Vector2.md) as -1, 0
or 1.

```lua
print(Vector2.new(-1.5, 2.25):Sign()) --> -1, 1
print(Vector2.new(-3, 0):Sign()) --> -1, 0
```

### Vector2:Angle

**Signature:** `Vector2:Angle(other: Vector2, isSigned?: boolean): number`

Returns the angle in radians between the two vectors. Specify `true` for
the optional `isSigned` boolean if you want a signed angle. By default,
the method returns the absolute value. Signed angles are a negative when
going clockwise. Values are in the range `[0, pi]` for absolute angles and
`[-pi, pi]` for signed angles.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `other` | `Vector2` |  | The vector to measure the angle to. |
| `isSigned` | `boolean` | `false` | Whether to return a signed angle (negative when clockwise) instead of the absolute value. |

**Returns:** `number` — The angle in radians between the two vectors, in the range [0, pi] for
unsigned or [-pi, pi] for signed.

**Vector2:Angle()**

Shows how to get the angle in radians between two [Vector2](/docs/reference/engine/datatypes/Vector2.md) values,
and how to ask for a signed angle.

```lua
local right = Vector2.xAxis
local up = Vector2.yAxis

print(math.round(math.deg(right:Angle(up)))) --> 90

-- By default the angle is never negative. A signed angle is negative when it turns clockwise.
print(math.round(math.deg(right:Angle(up, true)))) --> 90
print(math.round(math.deg(up:Angle(right, true)))) --> -90
```

### Vector2:Dot

**Signature:** `Vector2:Dot(v: Vector2): number`

Returns the scalar dot product of this vector and `v`, computed as
`self.X * v.X + self.Y * v.Y`. The result equals the product of the two
magnitudes multiplied by the cosine of the angle between them. A positive
value means the vectors point in roughly the same direction; zero means
they are perpendicular; negative means they point in roughly opposite
directions.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `v` | `Vector2` |  | The vector to compute the dot product with. |

**Returns:** `number` — The scalar dot product of the two vectors, equal to
`self.X * v.X + self.Y * v.Y`.

**Vector2:Dot()**

Shows how the dot product of two [Vector2](/docs/reference/engine/datatypes/Vector2.md) values is positive, zero
or negative depending on how much they point the same way.

```lua
local right = Vector2.xAxis

print(right:Dot(Vector2.new(2, 0))) --> 2
print(right:Dot(Vector2.yAxis)) --> 0
print(right:Dot(Vector2.new(-3, 0))) --> -3

-- Dotted with a unit vector, it gives how far a vector reaches along that direction.
print(Vector2.new(3, 4):Dot(Vector2.xAxis)) --> 3

-- A vector dotted with itself is its magnitude squared.
print(Vector2.new(3, 4):Dot(Vector2.new(3, 4))) --> 25
```

### Vector2:Lerp

**Signature:** `Vector2:Lerp(v: Vector2, alpha: number): Vector2`

Returns a [Vector2](/docs/reference/engine/datatypes/Vector2.md) linearly interpolated between this
[Vector2](/docs/reference/engine/datatypes/Vector2.md) and the given goal `v` by the fraction `alpha`, using
the formula `self + (v - self) * alpha`. An `alpha` of 0 returns `self`,
an `alpha` of 1 returns `v`, and values between 0 and 1 return a point in
between. Note that `alpha` is **not** clamped to `[0, 1]` — values outside
that range extrapolate beyond the two endpoints.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `v` | `Vector2` |  | The goal vector to interpolate toward. |
| `alpha` | `number` |  | The interpolation fraction, where 0 returns this vector and 1 returns `v`. |

**Returns:** `Vector2` — A vector linearly interpolated between this vector and `v` by the
fraction `alpha`.

**Vector2:Lerp()**

Shows how to blend between two [Vector2](/docs/reference/engine/datatypes/Vector2.md) values with an alpha from 0
to 1.

```lua
local start = Vector2.new(0, 0)
local goal = Vector2.new(10, 20)

print(start:Lerp(goal, 0)) --> 0, 0
print(start:Lerp(goal, 0.25)) --> 2.5, 5
print(start:Lerp(goal, 1)) --> 10, 20
```

### Vector2:Max

**Signature:** `Vector2:Max(others...: Tuple): Vector2`

Returns a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component as the highest among the
respective components of the provided [Vector2](/docs/reference/engine/datatypes/Vector2.md) objects.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `others...` | `Tuple` |  | One or more [Vector2](/docs/reference/engine/datatypes/Vector2.md) objects to compare against. |

**Returns:** `Vector2` — A vector whose each component is the highest value found among this
vector and all provided vectors.

**Vector2:Max()**

Shows how to take the larger value of each component from two
[Vector2](/docs/reference/engine/datatypes/Vector2.md) values.

```lua
local a = Vector2.new(-1.5, 2.25)
local b = Vector2.new(0, 5)
print(a:Max(b)) --> 0, 5
print(Vector2.new(4, 1):Max(Vector2.new(2, 3))) --> 4, 3
```

### Vector2:Min

**Signature:** `Vector2:Min(others...: Tuple): Vector2`

Returns a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component as the lowest among the
respective components of the provided [Vector2](/docs/reference/engine/datatypes/Vector2.md) objects.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `others...` | `Tuple` |  | One or more [Vector2](/docs/reference/engine/datatypes/Vector2.md) objects to compare against. |

**Returns:** `Vector2` — A vector whose each component is the lowest value found among this
vector and all provided vectors.

**Vector2:Min()**

Shows how to take the smaller value of each component from two
[Vector2](/docs/reference/engine/datatypes/Vector2.md) values.

```lua
local a = Vector2.new(-1.5, 2.25)
local b = Vector2.new(0, 5)
print(a:Min(b)) --> -1.5, 2.25
print(Vector2.new(4, 1):Min(Vector2.new(2, 3))) --> 2, 1
```

### Vector2:FuzzyEq

**Signature:** `Vector2:FuzzyEq(other: Vector2, epsilon?: number): bool`

Returns `true` if the X and Y components of the other [Vector2](/docs/reference/engine/datatypes/Vector2.md)
are within `epsilon` units of each corresponding component of this
[Vector2](/docs/reference/engine/datatypes/Vector2.md). The comparison is per-component: both
`math.abs(self.X - other.X)` and `math.abs(self.Y - other.Y)` must be less
than or equal to `epsilon`. The optional `epsilon` parameter defaults to
`1e-5` (0.00001) and must be a positive value no greater than `0.1`.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `other` | `Vector2` |  | The vector to compare against. |
| `epsilon` | `number` | `0.00001 (1e-5)` | The maximum difference allowed for each component to still be considered equal. |

**Returns:** `bool` — `true` if every component of this vector is within `epsilon` of the
corresponding component of `other`.

**Vector2:FuzzyEq()**

Shows how to compare two [Vector2](/docs/reference/engine/datatypes/Vector2.md) values with a tolerance instead of
an exact match.

```lua
local measured = Vector2.new(1.001, 2)
print(measured == Vector2.new(1, 2)) --> false

-- FuzzyEq is true when each component differs by no more than the tolerance.
print(measured:FuzzyEq(Vector2.new(1, 2), 0.01)) --> true
print(measured:FuzzyEq(Vector2.new(1, 2), 0.0001)) --> false

-- Without a tolerance it uses 1e-5.
print(Vector2.new(1.000009, 2.000009):FuzzyEq(Vector2.new(1, 2))) --> true
print(Vector2.new(1.001, 2):FuzzyEq(Vector2.new(1, 2))) --> false
```

## Math Operations

| Operation | Type A | Type B | Returns | Description |
|-----------|--------|--------|---------|-------------|
| `+` | `Vector2` | `Vector2` | `Vector2` | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component of the second added to the corresponding component of the first. |
| `-` | `Vector2` | `Vector2` | `Vector2` | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component of the second subtracted from the corresponding component of the first. |
| `*` | `Vector2` | `Vector2` | `Vector2` | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component of the second multiplied by the corresponding component of the first. |
| `/` | `Vector2` | `Vector2` | `Vector2` | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component of the first divided by the corresponding component of the second. |
| `*` | `Vector2` | `number` | `Vector2` | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component multiplied by the number. |
| `/` | `Vector2` | `number` | `Vector2` | Produces a [Vector2](/docs/reference/engine/datatypes/Vector2.md) with each component divided by the number. |