> For the complete documentation index, see [llms.txt](https://docs.anvil.team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.anvil.team/types/vec3.md).

# Vec3

### Properties

* <mark style="color:purple;">`x`</mark> - double
* <mark style="color:purple;">`y`</mark> - double
* <mark style="color:purple;">`z`</mark> - double

### Methods

* [constructor](#vec2-new-x-y)
* [length](#length)
* [distanceTo](#distanceto-othervec2)
* [distanceTo2D](#distanceto2d-othervec3)
* [getXYAngle](#getxyangle)
* [normalize360](#normalize360)
* [normalized](#normalized)
* [scale](#scale-scalar)
* [screenPosition](#screenposition)
* [toVec2](#scale-scalar-1)
* [toString](#tostring)

### <mark style="color:green;">`Vec3:new(x, y, z)`</mark>

* <mark style="color:purple;">`x`</mark> - What to set x to
* <mark style="color:purple;">`y`</mark> - What to set y to&#x20;
* <mark style="color:purple;">`z`</mark> - What to set z to&#x20;

Constructs a new Vec3 with the provided x, y and z.

**Example:**

```lua
local worldPosition = Vec3:new(100, 200, 300)
```

### <mark style="color:green;">`length()`</mark>

Returns the length of the Vec3

**Example:**

```lua
local vec = Vec3:new(200, 200, 100)
print("The vector has a length of " .. vecL:length())
```

### <mark style="color:green;">`distanceTo(otherVec3)`</mark>

* <mark style="color:purple;">`otherVec3`</mark> - A different Vec3&#x20;

Returns the distance from this vector to the `otherVec3`

**Example:**

```lua
local vecA = Vec3:new(200, 200, 20)
local vecB = Vec3:new(400, 690, 1000)
print("Distance from vecA to vecB: " .. vecA:distanceTo(vecB))
```

### <mark style="color:green;">`distanceTo2D(otherVec3)`</mark>

* <mark style="color:purple;">`otherVec3`</mark> - A different Vec3&#x20;

Returns the 2D distance from this vector to the `otherVec3`

**Example:**

```lua
local vecA = Vec2:new(1, 0)
local vecB = Vec2:new(0, 1)
print("2D Distance from vecA to vecB: " .. vecA:distanceTo2D(vecB)) 
```

### <mark style="color:green;">`getXYAngle()`</mark>

Returns the angle which this vector is pointing at in the XY-Plane.

**Example:**

```lua
local vecA = Vec2:new(1, 1, 0)
print("vecA angle: " .. vecA:getXYAngle()) -- prints 45.0
```

### <mark style="color:green;">`normalize360()`</mark>

Returns a new Vec3 where all components are normalized between 0 and 360.\
Useful when working with viewangles.

**Example:**

```lua
local vecA = Vec2:new(1, 1, 0)
print("vecA angle: " .. vecA:getXYAngle()) -- prints 45.0
```

### <mark style="color:green;">`normalized()`</mark>

Returns this vector but normalized (that is with length 1)

**Example:**

```lua
local vecA = Vec3:new(20, 10, 10)
print("vecA normalized: " .. vecA:normalized():toString())
-- prints vecA normalized: (+0.81649661, +0.40824831, +0.40824831)
```

### <mark style="color:green;">`scale(scalar)`</mark>

* <mark style="color:purple;">`scalar`</mark> - By how much you want to scale the vector

Returns a scaled version of this vector

**Example:**

```lua
local vecA = Vec3:new(20, 10, 5)
print("vecA scaled by 2: " .. vecA:scale(2):toString()) --prints (40, 20, 10)
```

### <mark style="color:green;">`screenPosition()`</mark>

Returns a Vec2 of where this Vec3 would be drawn on screen if it was a world position.

**Example:**

```lua
local closest = engine.getClosestPlayer().lerpOrigin
print("closest player head screen pos: " .. closest:screenPosition():toString())
```

### <mark style="color:green;">`toVec2()`</mark>

Returns the first 2 components of this vector as a Vec2

**Example:**

```lua
local vecA = Vec3:new(20, 10, 5)
local vecA2D = vecA:toVec2()
print("vecA2D : " .. vecA2D:toString()) --prints (20, 10)
```

### <mark style="color:green;">`toString()`</mark>

Returns a string representation of the Vec3

**Example:**

```lua
local vecA = Vec3:new(20, 10, 5)
print("vecA: " .. vecA:toString()) --prints (20, 10, 5)
```
