Vectors

Vectors are an important fundamental structure in Unity and in general when it comes to working with graphics. The x, y, z axis are color coded as RGB and are represented as vectors (right, up, forward) in Unity. Vectors are typically utilized in 2D and 3D space although the concept of vectors is applicable to any dimension.

Normalization

It is useful to normalize vectors, that is the length of the vector is one. The length of the vector can be found by applying the pythagorean theorem or by using the magnitude property. This is useful as anything multiplied by one gives the value it was applied to meaning consistent movement in space can be applied such as moving two units in a given direction for whatever unit you may use such as meters per second.

// unity where pt is a Vector2
Vector2 normalized = pt.normalized;

// manual calculation
// this would have to handle division by zero
Vector2 normalized = pt / pt.magnitude;

Add

The addition of two vectors is commutative and is done by adding one vector to the end of the other. This can be done component wise (a.x + b.x, a.y + b.y). For example, a vector in 2D otherwise known as a Vector2 of a = (2, 3) and b = (1, 2) will give the vector of a + b = (3, 5).

Subtract

Subtraction is a special case of addition where you negate one and add thus it is also done component wise. However, it is not commutative. It is important to note that the length of the resulting difference vector is the distance between the two points.

// unity built in method
Vector2.Distance(a, b);

// the magnitude (length) of the difference vector
float distance = (a - b).magnitude;

// manual calculation using pythagorean theorem
// to get length of the difference vector
// sqrt((a.x - b.x)^2 + (a.y - b.y)^2);

// the sqrt can be avoided by using the squared values
// instead for when exact distance is not needed
// such as with threshold checks
pt.sqrMagnitude;

// the above is equivalent to
Vector2.Dot(pt, pt);

In order to get the direction of the distance of the two points, the order of the points are flipped. The direction a to b is b - a and b to a is a - b.

Dot Product

The dot product between two vectors is a scalar projection meaning it projects one vector onto another giving a length as the result. This projection is only true when at least one of the vectors is normalized and the other vector is being projected onto the normalized vector otherwise it is a not really a projection but a projection and scale. Regardless, the result will be the same no matter which one is used. It is just easier to geometrically represent and see the result for the former. The most common use cases involve at least one of the vectors being normalized. The dot product is useful when used in calculating something in relation to an axis such as the sound of a ball hitting the normal of a surface from different angles.

// unity
Vector2.Dot(a, b);

// manual calculation
float length = (a.x * b.x) + (a.y * b.y)

Cross Product

The cross product returns a perpendicular vector to the two vectors used. For instance, the tangent and the bitangent will give the normal. It can thought of as x and y giving z in terms of axis.

// unity
Vector3.Cross(a, b);

Note that the vector that is produced will only be normalized if both inputs are perpendicular to one another. Also, the order of the inputs matters in terms of which direction the output vector points in.

It is useful to remember that Unity is a left handed Y up coordinate system when visualizing cross product by using your left hand thumb as the x, index as y, and middle as z. For rotations, a thumbs up can be used to determine a positive rotation for an axis where rotation towards the curled fingers is positive.

Component Wise Multiplication

Not commonly used. It is a special form used for scaling and is done by calling the scale method for a vector.