*need*to know this stuff) it looks like I need to understand them, backwards!

A Vector is a line drawn between two points in 2D or 3D space which also has a length, known as its magnitude.

A Vector always starts from it’s Origin – its [0,0] ([x,y]) or [0,0,0] ([x,y,z]) component. And consists of *two* components – the first being the [0,0] ([x,y]) or [0,0,0] ([x,y,z]) component and the second being set programmatically [n,n] ([x,y]) or [n,n,n] ([x,y,z]).

Example of a Vector setups:

//A 2D Vector v2 being constructed //The Origin is 0,0 and the point? is 6,4 in the x y axis Vector2 v2 = new Vector2((float)6.0f, (float)4.0f);

//A 3D Vector v3 being constructed //The Origin is 0,0,0 and the point? is 6,0,4 in the x y z axis Vector3 v3 = new Vector3((float)6.0f,(float)0.0f,(float)4.0f);

Therefore in the 2D Vector (I’m all out of 3D paper);

*x² + y² = Magnitude²*

*Magnitude = √x² + y²*

*Magnitude = √6² + 2²*

*Magnitude = √36 + 4*

*Magnitude = √40*

*Magnitude = 6.325*

**So the Magnitude of v2 is 6.325**

Instead of doing these calculations in Unity the long way, the engine comes with it’s own pre-canned magnitude function attached to both the Vector2 and Vector3 classes.

//Pre-canned magnitude function Vector2.magnitude; Vector3.magnitude;

There are two common operators involving Vectors – dot product and cross product.

## Dot Product

The Dot Product is the combined value of two Vectors of the same type which produce a single value known as a **scalar**.

Vector1 (x,y,z)

Vector2 (x,y,z)

(1x * 2x) + (1y * 2y) + (1z * 2z) = the Dot Product of two Vector3’s

Vector3 (x,y)

Vector4 (x,y)

(3x * 4x) + (3y * 4y) = the Dot Product of two Vector2’s

**But why do we need this?**

If the Dot Product of two vectors = 0, we know that the two vectors are perpendicular (i.e. at an angle of 90° to each other) Unity also comes with a handy built in function similar to that of the magnitude function.

/Dot product in built function Vector3.Dot(Vector1, Vector2) Vector2.Dot(Vector3, Vector4)

Broadly speaking, if the dot product of two non-zero vectors is positive, then the two vectors point in the same general direction, meaning less than 90 degrees. If the dot product is negative, then the two vectors point in opposite directions, or above 90 and less than or equal to 180 degrees.

## Cross Product

The cross product produces a single vector from two input vectors. It’s called a cross product because its the product of a cross over. It’s a little harder to explain than the Dot Product but here goes.

Vector1 ^ Vector2 = Cross Product Vector

Vector1 x Vector2 x

Vector1 y Vector2 y

Vector1 z Vector2 z

—————————

Vector1 x Vector2 x

Vector1 y Vector2 y

Vector1 z Vector2 z

(Vector1 y * Vector2 z) – (Vector1 z * Vector2 y) = Cross Vector x

(Vector1 z * Vector2 x) – (Vector1 x * Vector2 z) = Cross Vector y

(Vector1 x * Vector2 y) – (Vector1 y * Vector2 x) = Cross Vector z

You’ll notice that the Cross Product vector is created by the x weight from the origin two vectors y and z weights. The y weight is created by the x and z weights, and the z weight is created by the x and y weights.

Thankfully Unity saves us some time with this mind bending mathematics with a pre-built function!

//Cross product in built function Vector3.Cross(Vector1, Vector2)