# Vector

Kind: Struct · Namespace: Sansar · Assembly: Sansar.Script.dll · Script API 47.10.0

<div class="api-meta">
<span class="api-kind api-kind-struct" data-pagefind-filter="kind:Struct">Struct</span>
<span data-pagefind-filter="namespace:Sansar">Namespace <a href="/scripting/api/sansar"><code>Sansar</code></a></span>
</div>

<span id="T:Sansar.Vector"></span>

The Vector class represents a 3 dimensional vector.

```csharp
public struct Vector
```

## Members at a glance

| Kind | Members |
|---|---|
| **Constructors** | [Vector](#vector) |
| **Fields** | [Back](#back) · [Down](#down) · [Forward](#forward) · [Left](#left) · [ObjectBack](#objectback) · [ObjectDown](#objectdown) · [ObjectForward](#objectforward) · [ObjectLeft](#objectleft) · [ObjectRight](#objectright) · [ObjectUp](#objectup) · [One](#one) · [Right](#right) · [Up](#up) · [Zero](#zero) |
| **Properties** | [Item](#item) · [W](#w) · [X](#x) · [Y](#y) · [Z](#z) |
| **Methods** | [AngleTo](#angleto) · [Cross](#cross) · [Dot](#dot) · [Dot3](#dot3) · [Dot4](#dot4) · [Length](#length) · [LengthSquared](#lengthsquared) · [Lerp](#lerp) · [Normalized](#normalized) · [Orthonormalize](#orthonormalize) · [Parse](#parse) · [Rotate](#rotate) · [ToString](#tostring) · [ToString4](#tostring4) · [TryParse](#tryparse) |
| **Operators** | [op\_Addition](#op_addition) · [op\_Division](#op_division) · [op\_Implicit](#op_implicit) · [op\_Multiply](#op_multiply) · [op\_Subtraction](#op_subtraction) |

## Constructors

### Vector

<div class="api-card" id="ctor-vector4f">
<span id="M:Sansar.Vector.#ctor(Mono.Simd.Vector4f@)"></span>
<span id="C:Sansar.Vector(Mono.Simd.Vector4f@)"></span>

```csharp signature
public Vector(
    [System.Runtime.CompilerServices.IsReadOnly] ref Mono.Simd.Vector4f v
)
```

Creates a new vector with the supplied values.

| Parameter | Type | Description |
|---|---|---|
| `v` | Mono.Simd.Vector4f | Initializes the vector from a `Vector4f`. W should be 0 and is assumed to be 0 for most operations. |

</div>

<div class="api-card" id="ctor-single">
<span id="M:Sansar.Vector.#ctor(System.Single)"></span>
<span id="C:Sansar.Vector(System.Single)"></span>

```csharp signature
public Vector(float all)
```

Creates a new vector with all values set to the given value.

| Parameter | Type | Description |
|---|---|---|
| `all` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The value for all coordinates. |

<details class="api-remarks"><summary>Remarks</summary>

W will be set to 0.

</details>

</div>

<div class="api-card" id="ctor-single-single-single-single">
<span id="M:Sansar.Vector.#ctor(System.Single,System.Single,System.Single,System.Single)"></span>
<span id="C:Sansar.Vector(System.Single,System.Single,System.Single,System.Single)"></span>

```csharp signature
public Vector(float x, float y, float z, float w)
```

Creates a new vector with the supplied values.

| Parameter | Type | Description |
|---|---|---|
| `x` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The x coordinate. |
| `y` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The y coordinate. |
| `z` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The z coordinate. |
| `w` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The w coordinate. W defaults to 0 and is assumed to be 0 for most operations. |

</div>


## Fields

### Back

<div class="api-card">
<span id="F:Sansar.Vector.Back"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Use ObjectBack for correct back vector.", false)]
public static readonly Vector Back
```

Deprecated back vector.

</div>

### Down

<div class="api-card">
<span id="F:Sansar.Vector.Down"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Use ObjectDown for correct down vector.", false)]
public static readonly Vector Down
```

Deprecated down vector.

</div>

### Forward

<div class="api-card">
<span id="F:Sansar.Vector.Forward"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Use ObjectForward for correct forward vector.", false)]
public static readonly Vector Forward
```

Deprecated forward vector.

</div>

### Left

<div class="api-card">
<span id="F:Sansar.Vector.Left"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Use ObjectLeft for correct left vector.", false)]
public static readonly Vector Left
```

Deprecated left vector.

</div>

### ObjectBack

<div class="api-card">
<span id="F:Sansar.Vector.ObjectBack"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 36.0.1</span></div>

```csharp signature
public static readonly Vector ObjectBack
```

The default world back vector.

</div>

### ObjectDown

<div class="api-card">
<span id="F:Sansar.Vector.ObjectDown"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 36.0.1</span></div>

```csharp signature
public static readonly Vector ObjectDown
```

The default world down vector.

</div>

### ObjectForward

<div class="api-card">
<span id="F:Sansar.Vector.ObjectForward"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 36.0.1</span></div>

```csharp signature
public static readonly Vector ObjectForward
```

The default world forward vector.

</div>

### ObjectLeft

<div class="api-card">
<span id="F:Sansar.Vector.ObjectLeft"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 36.0.1</span></div>

```csharp signature
public static readonly Vector ObjectLeft
```

The default world left vector.

</div>

### ObjectRight

<div class="api-card">
<span id="F:Sansar.Vector.ObjectRight"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 36.0.1</span></div>

```csharp signature
public static readonly Vector ObjectRight
```

The default world right vector.

</div>

### ObjectUp

<div class="api-card">
<span id="F:Sansar.Vector.ObjectUp"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 36.0.1</span></div>

```csharp signature
public static readonly Vector ObjectUp
```

The default world up vector.

</div>

### One

<div class="api-card">
<span id="F:Sansar.Vector.One"></span>

```csharp signature
public static readonly Vector One
```

A vector with all components 1.

</div>

### Right

<div class="api-card">
<span id="F:Sansar.Vector.Right"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Use ObjectRight for correct right vector.", false)]
public static readonly Vector Right
```

Deprecated right vector.

</div>

### Up

<div class="api-card">
<span id="F:Sansar.Vector.Up"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Use ObjectUp for correct up vector.", false)]
public static readonly Vector Up
```

Deprecated up Vector

</div>

### Zero

<div class="api-card">
<span id="F:Sansar.Vector.Zero"></span>

```csharp signature
public static readonly Vector Zero
```

A vector with all components 0.

</div>


## Properties

### Item

<div class="api-card">
<span id="P:Sansar.Vector.Item(System.Int32)"></span>

```csharp signature
public float this [int index] { get; set; }
```

Allows getting coordinates by index.

| Parameter | Type | Description |
|---|---|---|
| `index` | [int](https://learn.microsoft.com/dotnet/api/system.int32) | 0=&gt;X, 1=&gt;Y, 2=&gt;Z, 3=&gt;W |

</div>

### W

<div class="api-card">
<span id="P:Sansar.Vector.W"></span>

```csharp signature
public float W { get; set; }
```

The W coordinate of the vector.

<details class="api-remarks"><summary>Remarks</summary>

W should be 0 and is assumed to be 0 for most operations.

</details>

</div>

### X

<div class="api-card">
<span id="P:Sansar.Vector.X"></span>

```csharp signature
public float X { get; set; }
```

The X coordinate of the vector.

</div>

### Y

<div class="api-card">
<span id="P:Sansar.Vector.Y"></span>

```csharp signature
public float Y { get; set; }
```

The Y coordinate of the vector.

</div>

### Z

<div class="api-card">
<span id="P:Sansar.Vector.Z"></span>

```csharp signature
public float Z { get; set; }
```

The Z coordinate of the vector.

</div>


## Methods

### AngleTo

<div class="api-card">
<span id="M:Sansar.Vector.AngleTo(Sansar.Vector@)"></span>

```csharp signature
public float AngleTo(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b
)
```

Returns the angle in radians between this vector and the given vector.

| Parameter | Type | Description |
|---|---|---|
| `b` | [Vector](/scripting/api/sansar/vector) | The angle to compare. |

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — The smallest angle in radians between the two vectors.

</div>

### Cross

<div class="api-card">
<span id="M:Sansar.Vector.Cross(Sansar.Vector@)"></span>

```csharp signature
public Vector Cross(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b
)
```

Calculates the cross product of 2 3D vectors.

| Parameter | Type | Description |
|---|---|---|
| `b` | [Vector](/scripting/api/sansar/vector) | The second vector. |

**Returns:** [Vector](/scripting/api/sansar/vector) — a X b

</div>

### Dot

<div class="api-card">
<span id="M:Sansar.Vector.Dot(Sansar.Vector@)"></span>

```csharp signature
public float Dot(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b
)
```

Performs a 3-component scalar or dot product.

| Parameter | Type | Description |
|---|---|---|
| `b` | [Vector](/scripting/api/sansar/vector) | The second vector. |

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — Returns X\*b.X+Y\*b.Y+Z\*b.Z

</div>

### Dot3

<div class="api-card">
<span id="M:Sansar.Vector.Dot3(Sansar.Vector@)"></span>

```csharp signature
public float Dot3(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b
)
```

Performs a 3-component scalar or dot product.

| Parameter | Type | Description |
|---|---|---|
| `b` | [Vector](/scripting/api/sansar/vector) | The second vector. |

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — Returns X\*b.X+Y\*b.Y+Z\*b.Z

</div>

### Dot4

<div class="api-card">
<span id="M:Sansar.Vector.Dot4(Sansar.Vector@)"></span>

```csharp signature
public float Dot4(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b
)
```

Performs a 4-component scalar or dot product.

| Parameter | Type | Description |
|---|---|---|
| `b` | [Vector](/scripting/api/sansar/vector) | The second vector. |

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — Returns X\*b.X+Y\*b.Y+Z\*b.Z+W\*b.W

</div>

### Length

<div class="api-card">
<span id="M:Sansar.Vector.Length"></span>
<span id="M:Sansar.Vector.Length()"></span>

```csharp signature
public float Length()
```

Calculates the magnitude of this vector.

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — The 3D vector length.

</div>

### LengthSquared

<div class="api-card">
<span id="M:Sansar.Vector.LengthSquared"></span>
<span id="M:Sansar.Vector.LengthSquared()"></span>

```csharp signature
public float LengthSquared()
```

Calculates the square magnitude of this vector.

**Returns:** [float](https://learn.microsoft.com/dotnet/api/system.single) — The square of the 3D vector length.

</div>

### Lerp

<div class="api-card">
<span id="M:Sansar.Vector.Lerp(Sansar.Vector@,System.Single)"></span>

```csharp signature
public Vector Lerp(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b,
    float amount
)
```

Performs a linear interpolation between two vectors.

| Parameter | Type | Description |
|---|---|---|
| `b` | [Vector](/scripting/api/sansar/vector) | The second vector. |
| `amount` | [float](https://learn.microsoft.com/dotnet/api/system.single) | Value from \[0..1\] indicating the weight for the second vector. |

**Returns:** [Vector](/scripting/api/sansar/vector) — A vector that is linearly interpolated between the two sources.

<details class="api-remarks"><summary>Remarks</summary>

This method performs the linear interpolation as:

a + (b-a) \* amount.

 An amount value of 0 returns vector a, and 1 returns vector b.

</details>

</div>

### Normalized

<div class="api-card">
<span id="M:Sansar.Vector.Normalized"></span>
<span id="M:Sansar.Vector.Normalized()"></span>

```csharp signature
public Vector Normalized()
```

Returns a vector with the same orientation and unit length.

**Returns:** [Vector](/scripting/api/sansar/vector) — A vector with the same orientation and unit length.

</div>

### Orthonormalize

<div class="api-card">
<span id="M:Sansar.Vector.Orthonormalize(Sansar.Vector@,Sansar.Vector@)"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 35.0.0</span></div>

```csharp signature
public static Vector Orthonormalize(ref Vector forward, ref Vector up)
```

Normalizes the two vectors and returns the normalized cross product.

| Parameter | Type | Description |
|---|---|---|
| `forward` | [Vector](/scripting/api/sansar/vector) | The facing ve |
| `up` | [Vector](/scripting/api/sansar/vector) |  |

**Returns:** [Vector](/scripting/api/sansar/vector)

</div>

### Parse

<div class="api-card">
<span id="M:Sansar.Vector.Parse(System.String)"></span>

```csharp signature
public static Vector Parse(string vectorString)
```

Parse a Vector from a string.

| Parameter | Type | Description |
|---|---|---|
| `vectorString` | [string](https://learn.microsoft.com/dotnet/api/system.string) | A string of the format &lt;X,Y,Z&gt; or &lt;X,Y,Z,W&gt; |

**Returns:** [Vector](/scripting/api/sansar/vector) — The Vector parsed from the string.

**Throws** [`ArgumentException`](https://learn.microsoft.com/dotnet/api/system.argumentexception): If vectorString is null.

**Throws** [`FormatException`](https://learn.microsoft.com/dotnet/api/system.formatexception): If the string is not a valid vector or its components are not valid floats.

<details class="api-example" open><summary>Example</summary>

```csharp
Vector myVector = Vector.Parse("<1,2.34,5.6>");
```

</details>

</div>

### Rotate

<div class="api-card">
<span id="M:Sansar.Vector.Rotate(Sansar.Quaternion@)"></span>

```csharp signature
public Vector Rotate(
    [System.Runtime.CompilerServices.IsReadOnly] ref Quaternion q
)
```

Returns a new vector which is this vector rotated by the given quaternion.

| Parameter | Type | Description |
|---|---|---|
| `q` | [Quaternion](/scripting/api/sansar/quaternion) | The quaternion rotation. |

**Returns:** [Vector](/scripting/api/sansar/vector)

</div>

### ToString

<div class="api-card" id="tostring-noargs">
<span id="M:Sansar.Vector.ToString"></span>
<span id="M:Sansar.Vector.ToString()"></span>

```csharp signature
public override string ToString()
```

Generates a string representation of the vector.

**Returns:** [string](https://learn.microsoft.com/dotnet/api/system.string) — The vector as a string.

</div>

<div class="api-card" id="tostring-string">
<span id="M:Sansar.Vector.ToString(System.String)"></span>

```csharp signature
public string ToString(string format)
```

Generates a string representation of the vector.

| Parameter | Type | Description |
|---|---|---|
| `format` | [string](https://learn.microsoft.com/dotnet/api/system.string) | Format to use for each of the coordinates. |

**Returns:** [string](https://learn.microsoft.com/dotnet/api/system.string) — The vector as a string.

</div>

### ToString4

<div class="api-card">
<span id="M:Sansar.Vector.ToString4(System.String)"></span>

```csharp signature
public string ToString4(string format)
```

Generates a string representation of the vector, including the W parameter.

| Parameter | Type | Description |
|---|---|---|
| `format` | [string](https://learn.microsoft.com/dotnet/api/system.string) | Format to use for each of the coordinates. |

**Returns:** [string](https://learn.microsoft.com/dotnet/api/system.string) — The vector as a string.

</div>

### TryParse

<div class="api-card">
<span id="M:Sansar.Vector.TryParse(System.String,Sansar.Vector@)"></span>

```csharp signature
public static bool TryParse(string vectorString, out Vector vector)
```

Attempt to parse a Vector from a string

| Parameter | Type | Description |
|---|---|---|
| `vectorString` | [string](https://learn.microsoft.com/dotnet/api/system.string) | A string of the format &lt;X,Y,Z&gt; or &lt;X,Y,Z,W&gt; |
| `vector` | [Vector](/scripting/api/sansar/vector) | The vector that will be set if vectorString represents a valid vector. |

**Returns:** [bool](https://learn.microsoft.com/dotnet/api/system.boolean) — True if successfully parsed a vector, false if not.

<details class="api-example" open><summary>Example</summary>

```csharp
Vector myVector;
if (Vector.Parse("<1,2.34,5.6>", out myVector)
{
    // myVector is set
}
```

</details>

</div>


## Operators

### op_Addition

<div class="api-card">
<span id="M:Sansar.Vector.op_Addition(Sansar.Vector@,Sansar.Vector@)"></span>

```csharp signature
public static Vector operator +(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector a,
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b
)
```

Performs vector addition.

| Parameter | Type | Description |
|---|---|---|
| `a` | [Vector](/scripting/api/sansar/vector) | The first Vector. |
| `b` | [Vector](/scripting/api/sansar/vector) | The second Vector. |

**Returns:** [Vector](/scripting/api/sansar/vector) — A new vector that is the sum of the arguments.

</div>

### op_Division

<div class="api-card">
<span id="M:Sansar.Vector.op_Division(Sansar.Vector@,System.Single)"></span>

```csharp signature
public static Vector operator /(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector a,
    float b
)
```

Divides vector components by a scalar.

| Parameter | Type | Description |
|---|---|---|
| `a` | [Vector](/scripting/api/sansar/vector) | The vector. |
| `b` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The scalar. |

**Returns:** [Vector](/scripting/api/sansar/vector) — Returns a new vector with value \[a.X/b, a.Y/b, a.Z/b, a.W/b\]

</div>

### op_Implicit

<div class="api-card" id="opimplicit-vector">
<span id="M:Sansar.Vector.op_Implicit(Mono.Simd.Vector4f@)~Sansar.Vector"></span>

```csharp signature
public static implicit operator Vector(
    [System.Runtime.CompilerServices.IsReadOnly] ref Mono.Simd.Vector4f v
)
```

Converts a Mono.Simd.Vector4f to a vector.

| Parameter | Type | Description |
|---|---|---|
| `v` | Mono.Simd.Vector4f | The vector to convert |

</div>

<div class="api-card" id="opimplicit-vector4f">
<span id="M:Sansar.Vector.op_Implicit(Sansar.Vector@)~Mono.Simd.Vector4f"></span>

```csharp signature
public static implicit operator Mono.Simd.Vector4f(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector v
)
```

Converts a vector to a Mono.Simd.Vector4f

| Parameter | Type | Description |
|---|---|---|
| `v` | [Vector](/scripting/api/sansar/vector) | The vector to convert |

</div>

### op_Multiply

<div class="api-card" id="opmultiply-vector-single">
<span id="M:Sansar.Vector.op_Multiply(Sansar.Vector@,System.Single)"></span>

```csharp signature
public static Vector operator *(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector a,
    float b
)
```

Performs a vector scalar multiplication.

| Parameter | Type | Description |
|---|---|---|
| `a` | [Vector](/scripting/api/sansar/vector) | The vector. |
| `b` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The scalar. |

**Returns:** [Vector](/scripting/api/sansar/vector) — Returns a new vector with value \[a.X\*b, a.Y\*b, a.Z\*b, a.W\*b\]

</div>

<div class="api-card" id="opmultiply-single-vector">
<span id="M:Sansar.Vector.op_Multiply(System.Single,Sansar.Vector@)"></span>

```csharp signature
public static Vector operator *(
    float a,
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b
)
```

Performs a vector scalar multiplication.

| Parameter | Type | Description |
|---|---|---|
| `a` | [float](https://learn.microsoft.com/dotnet/api/system.single) | The scalar. |
| `b` | [Vector](/scripting/api/sansar/vector) | The vector. |

**Returns:** [Vector](/scripting/api/sansar/vector) — Returns a new vector with value \[a\*b.X, a\*b.Y, a\*b.Z, a\*b.W\]

</div>

### op_Subtraction

<div class="api-card">
<span id="M:Sansar.Vector.op_Subtraction(Sansar.Vector@,Sansar.Vector@)"></span>

```csharp signature
public static Vector operator -(
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector a,
    [System.Runtime.CompilerServices.IsReadOnly] ref Vector b
)
```

Performs vector subtraction.

| Parameter | Type | Description |
|---|---|---|
| `a` | [Vector](/scripting/api/sansar/vector) | The first Vector. |
| `b` | [Vector](/scripting/api/sansar/vector) | The second Vector. |

**Returns:** [Vector](/scripting/api/sansar/vector) — A new vector that is the difference of the arguments.

</div>


## Used in examples

- [FollowThePath.cs](/scripting/examples/follow-the-path)
- [MoverExample1.cs](/scripting/examples/mover-examples)
- [MoverExample2.cs](/scripting/examples/mover-examples)
- [MoverExample3.cs](/scripting/examples/mover-examples)
- [RandomMovement.cs](/scripting/examples/random-movement)
- [BanHammerExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/BanHammerExample.cs)
- `CameraShakeExample.cs`
- [CastRayExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/CastRayExample.cs)
- `MoverExample4.cs`
- [ReactionsExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ReactionsExample.cs)
- [ScriptLibrary/Quest/QuestObjectBase.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Quest/QuestObjectBase.cs)
- [ScriptLibrary/Scene/Dispenser.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Dispenser.cs)

…and 8 more examples in the snapshot.

<p class="api-generated" data-pagefind-ignore>Generated from the Sansar 47.10.0 client ScriptApi snapshot.</p>