# Vector

This type is a common 3D vector (x, y, z).

{% hint style="info" %}
This type supports operations, such as `+`, `-`, `/`, `*` and `==`
{% endhint %}

## x﻿ <a href="#x" id="x"></a>

[<kbd><mark style="background-color:purple;">**Field**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Type: `float`

X coordinate.

## y﻿ <a href="#y" id="y"></a>

[<kbd><mark style="background-color:purple;">**Field**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Type: `float`

Y coordinate.

## z﻿ <a href="#z" id="z"></a>

[<kbd><mark style="background-color:purple;">**Field**<mark style="background-color:purple;"></kbd>](#user-content-fn-1)[^1]

Type: `float`

Z coordinate.

***

## \_\_call﻿ <a href="#call" id="call"></a>

[<kbd><mark style="background-color:purple;">**Constructor**<mark style="background-color:purple;"></kbd>](#user-content-fn-2)[^2]

Constructs a vector.

**Arguments**

*1. Default vector (0, 0, 0).*

None.

*2. Single value.*

| Name    | Type    | Description          |
| ------- | ------- | -------------------- |
| `value` | `float` | X and Y coordinates. |

*3. XYZ values.*

| Name | Type    | Description   |
| ---- | ------- | ------------- |
| `x`  | `float` | X coordinate. |
| `y`  | `float` | Y coordinate. |
| `z`  | `float` | Z coordinate. |

**Returns**

| Type     | Description |
| -------- | ----------- |
| `Vector` | Vector.     |

**Example**

```lua
local vec = Vector(5, 25, 12);
```

***

## IsZero <a href="#is-zero" id="is-zero"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns `true` if this vector is within tolerance range.

**Arguments**

| Name        | Type    | Description                                |
| ----------- | ------- | ------------------------------------------ |
| `tolerance` | `float` | Max allowed tolerance. Defaults to `0.01`. |

**Returns**

| Type   | Description                                            |
| ------ | ------------------------------------------------------ |
| `bool` | `true` if ranges between `-tolerance` and `tolerance`. |

**Example**

```lua
local vec = Vector(0, 0.1, 0.5);
print(tostring(vec:IsZero(1))); -- will print 'true', because every value fits in the range of -1 and 1
```

***

## Clone <a href="#length-2d-sqr" id="length-2d-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns a new copy of this vector.

**Arguments**

None.

**Returns**

| Type     | Description       |
| -------- | ----------------- |
| `Vector` | The copied vector |

**Example**

```lua
local a = Vector(1, 2, 3)
local b = a:Clone()
a.x = 0
b.x = 3 -- Changing B does not affect A
print(a.x, b.x) -- Prints 0 3
```

***

## Dist﻿ <a href="#dist" id="dist"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns distance to another vector.

**Arguments**

| Name    | Type     | Description                           |
| ------- | -------- | ------------------------------------- |
| `other` | `Vector` | Vector to calculate distance against. |

**Returns**

| Type    | Description               |
| ------- | ------------------------- |
| `float` | Distance to other vector. |

**Example**

```lua
local vec1 = Vector(0, 0, 0);
local vec2 = Vector(1, 1, 5);
print(tostring(vec1:Dist(vec2)));
```

***

## DistSqr <a href="#dist-sqr" id="dist-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns squared distance to another vector.

{% hint style="info" %}
This method is de-facto faster than the non-squared variant. Use it, if you need extra performance.
{% endhint %}

**Arguments**

| Name    | Type     | Description                           |
| ------- | -------- | ------------------------------------- |
| `other` | `Vector` | Vector to calculate distance against. |

**Returns**

| Type    | Description                       |
| ------- | --------------------------------- |
| `float` | Squared distance to other vector. |

**Example**

```lua
local vec1 = Vector(0, 0, 0);
local vec2 = Vector(1, 1, 5);
print(tostring(vec1:DistSqr(vec2)));
```

***

## Dist2d <a href="#dist-2d" id="dist-2d"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns 2D (only `x` and `y` values) distance to another vector.

**Arguments**

| Name    | Type     | Description                           |
| ------- | -------- | ------------------------------------- |
| `other` | `Vector` | Vector to calculate distance against. |

**Returns**

| Type    | Description               |
| ------- | ------------------------- |
| `float` | Distance to other vector. |

**Example**

```lua
local vec1 = Vector(0, 0, 0);
local vec2 = Vector(1, 1, 5);
print(tostring(vec1:Dist2d(vec2)));
```

***

## Dist2dSqr <a href="#dist-2d-sqr" id="dist-2d-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns squared 2D (only `x` and `y` values) distance to another vector.

{% hint style="info" %}
This method is de-facto faster than the non-squared variant. Use it, if you need extra performance.
{% endhint %}

**Arguments**

| Name    | Type     | Description                           |
| ------- | -------- | ------------------------------------- |
| `other` | `Vector` | Vector to calculate distance against. |

**Returns**

| Type    | Description                       |
| ------- | --------------------------------- |
| `float` | Squared distance to other vector. |

**Example**

```lua
local vec1 = Vector(0, 0, 0);
local vec2 = Vector(1, 1, 5);
print(tostring(vec1:Dist2dSqr(vec2)));
```

***

## Cross﻿ <a href="#cross" id="cross"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns a cross product to another vector.

**Arguments**

| Name    | Type     | Description                                |
| ------- | -------- | ------------------------------------------ |
| `other` | `Vector` | Vector to calculate cross product against. |

**Returns**

| Type     | Description    |
| -------- | -------------- |
| `Vector` | Cross product. |

**Example**

```lua
local vec1 = Vector(0, 0, 0);
local vec2 = Vector(1, 1, 5);
local cross = vec1:Cross﻿(vec2);
```

***

## IsValid <a href="#is-valid" id="is-valid"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns `true` if all values in this vector are finite.

**Arguments**

None.

**Returns**

| Type   | Description                  |
| ------ | ---------------------------- |
| `bool` | `true` if values are finite. |

**Example**

```lua
local vec = Vector(5, 1, 6);
if vec:IsValid() then
    -- ...
end
```

***

## Length <a href="#length" id="length"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns length of this vector.

**Arguments**

None.

**Returns**

| Type    | Description            |
| ------- | ---------------------- |
| `float` | Length of this vector. |

**Example**

```lua
local vec = Vector(5, 1, 6);
local length = vec:Length();
```

***

## LengthSqr <a href="#length-sqr" id="length-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns squared length of this vector.

{% hint style="info" %}
This method is de-facto faster than the non-squared variant. Use it, if you need extra performance.
{% endhint %}

**Arguments**

None.

**Returns**

| Type    | Description            |
| ------- | ---------------------- |
| `float` | Length of this vector. |

**Example**

```lua
local vec = Vector(5, 1, 6);
local length = vec:LengthSqr();
```

***

## Length2d

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns 2D length of this vector.

**Arguments**

None.

**Returns**

| Type    | Description            |
| ------- | ---------------------- |
| `float` | Length of this vector. |

**Example**

```lua
local vec = Vector(5, 1, 6);
local length = vec:Length2d();
```

***

## Length2dSqr

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns squared 2D length of this vector.

{% hint style="info" %}
This method is de-facto faster than the non-squared variant. Use it, if you need extra performance.
{% endhint %}

**Arguments**

None.

**Returns**

| Type    | Description            |
| ------- | ---------------------- |
| `float` | Length of this vector. |

**Example**

```lua
local vec = Vector(5, 1, 6);
local length = vec:Length2dSqr();
```

***

## Dot <a href="#dot" id="dot"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns dot product of 2 vectors.

**Arguments**

| Name    | Type     | Description                              |
| ------- | -------- | ---------------------------------------- |
| `other` | `Vector` | Vector to calculate dot product against. |

**Returns**

| Type    | Description  |
| ------- | ------------ |
| `float` | Dot product. |

**Example**

```lua
local vec1 = Vector(0, 0, 0);
local vec2 = Vector(1, 1, 5);
local dot = vec1:Dot(vec2);
```

***

## GetForward <a href="#length-2d-sqr" id="length-2d-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns the direction/forward vector of an angle.

**Arguments**

None.

**Returns**

| Type     | Description        |
| -------- | ------------------ |
| `Vector` | The forward vector |

**Example**

```lua
local view_angles = game.input:GetViewAngles()
local forward = view_angles:GetForward()
```

***

## AngleVectors <a href="#length-2d-sqr" id="length-2d-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns the forward, right and up vectors of an angle.

**Arguments**

None.

**Returns**

| Type     | Description        |
| -------- | ------------------ |
| `Vector` | The forward vector |
| `Vector` | The right vector   |
| `Vector` | The up vector      |

**Example**

```lua
local view_angles = game.input:GetViewAngles()
local forward, right, up = view_angles:AngleVectors()
```

***

## VectorAngles <a href="#length-2d-sqr" id="length-2d-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns the angle of a vector.

**Arguments**

None.

**Returns**

| Type     | Description |
| -------- | ----------- |
| `Vector` | The angles  |

**Example**

```lua
local velocity = entities.GetLocalPawn():GetAbsVelocity()
local direction = velocity:VectorAngles()
```

***

## Normalize <a href="#length-2d-sqr" id="length-2d-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Normalizes this vector, turning it into an unit/direction vector.

**Arguments**

None.

**Returns**

Nothing.

**Example**

```lua
local movement_dir = entities.GetLocalPawn():GetAbsVelocity()
movement_dir:Normalize()
```

***

## Normalized <a href="#length-2d-sqr" id="length-2d-sqr"></a>

[<kbd><mark style="background-color:purple;">**Method**<mark style="background-color:purple;"></kbd>](#user-content-fn-3)[^3]

Returns a normalized version of this vector, turning it into an unit/direction vector.

**Arguments**

None.

**Returns**

| Type     | Description           |
| -------- | --------------------- |
| `Vector` | The normalized vector |

**Example**

```lua
local movement_dir = entities.GetLocalPawn():GetAbsVelocity():Normalized()
```

[^1]: This field is a regular field that must be accessed using a dot (.)

[^2]: This is a constructor definition for this type.

[^3]: This field is a method and must be invoked using a colon (:)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lua2.fatality.win/api/types/vector.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
