### Vector Basics

A vector is a mathematical expression of both direction and magnitude. Vectors are often used for velocity (which requires both direction and magnitude). The individual parts of a vector are called components. For example a 2D vector would have an X and a Y component while a 3D vector would have an X, Y, and Z component.

In Unity vectors are declared like so:

Vector2 myVector = new Vector2(x,y);

Vector3 myVector = new Vector3(x,y,z);

The magnitude of a vector is the square root of the components squared and added together. So for a 2D vector with an X component of 3 and a Y component of 4 would have a magnitude of 5 (√32+42 = 5). Magnitude is shown with bars like this ||v||. This would mean the magnitude of vector v.

Sometimes vectors are shown with like this: v = 3i+4j. This would still mean a vector with an X component of 3 and Y component of 4. The i and j stand for unit vectors (vectors with a magnitude of 1).

We can quickly determine which way a vector is pointing by looking at the sign of the components. If the X component is positive the vector points right. If the X component is negative then the vector points left. For the Y component positive points up and negative points down.

We can manipulate vectors as well. Adding vectors is done by adding the X components and the Y components like this (vector u is equal to vector v plus t):

v = 3i + 4j

t = 2i + 5j

u = v + t

u = (3i + 2i, 4j + 5j) = 5i + 9j

We can also multiply vectors by scalar numbers (real numbers without direction). This is done by multiplying the individual components by the number. For example:

v = 5i + 2j

2v = (2*5i, 2*2j) = 10i + 4j

The new vector will have the same direction but twice the magnitude. If we were to multiply the vector by a negative number, the vector will flip direction (as well as change magnitude according to the number). We can also shorten a vector by multiplying it by a fraction.

In Unity vectors are declared like so:

Vector2 myVector = new Vector2(x,y);

Vector3 myVector = new Vector3(x,y,z);

The magnitude of a vector is the square root of the components squared and added together. So for a 2D vector with an X component of 3 and a Y component of 4 would have a magnitude of 5 (√32+42 = 5). Magnitude is shown with bars like this ||v||. This would mean the magnitude of vector v.

Sometimes vectors are shown with like this: v = 3i+4j. This would still mean a vector with an X component of 3 and Y component of 4. The i and j stand for unit vectors (vectors with a magnitude of 1).

We can quickly determine which way a vector is pointing by looking at the sign of the components. If the X component is positive the vector points right. If the X component is negative then the vector points left. For the Y component positive points up and negative points down.

We can manipulate vectors as well. Adding vectors is done by adding the X components and the Y components like this (vector u is equal to vector v plus t):

v = 3i + 4j

t = 2i + 5j

u = v + t

u = (3i + 2i, 4j + 5j) = 5i + 9j

We can also multiply vectors by scalar numbers (real numbers without direction). This is done by multiplying the individual components by the number. For example:

v = 5i + 2j

2v = (2*5i, 2*2j) = 10i + 4j

The new vector will have the same direction but twice the magnitude. If we were to multiply the vector by a negative number, the vector will flip direction (as well as change magnitude according to the number). We can also shorten a vector by multiplying it by a fraction.

More vector basics coming. I will also add some pictures. I'm working on a parkour engine in Unity which I will also post on here but it's important to understand how vectors work first.

## Comments

## Post a Comment