This tutorial is going to go over Vectors and how to use them in a program, specifically Unity. Vectors are very useful when moving objects in Unity as well as working with different Physics mechanics. In this tutorial we will firstly go over what a Vector is, then how to add and subtract vectors, multiply them using both dot and cross product, and finally what methods Unity has for calculating vectors.
A vector is a line in space which has both a magnitude and direction. The basic notation for a vector is an arrow whereas the line shows the length of the vector and the arrow points in the direction that it is traveling. Some example of vectors in the real world is the trajectory of a baseball traveling that was thrown and the path an apple takes as it falls from a tree. Vectors are generally named either having bolded text or contain a line over the name.
A 2D vector has both an X component and a Y component as shown below on the left. This is also known as standard form. The order that is traveled whether X then Y or Y then X does not change the final result F of the vector. One way of notating a 2D vector is (X,Y). Another way of notating a vector is (r,theta) where r is the radius of the vector, and theta is the angle at which the F is directed. This is also known as polar form. Both standard form and polar form have different math to calculate different mathematical operations. In this tutorial we will focus on the standard form for vectors.
Addition for two vectors is fairly straightforward. For standard form, we will add the X and Y components respectively. In the example below we are adding two vectors; A and B to get resultant vector C. For this tutorial we will denote the x part of a vector as a lower case x next to the name of the vector and the y part of a vector as a lower case y next to the name of the vector. Vector addition is commutative in that it does not matter if you added A to B or B to A. You would have gotten the same result.
Subtraction is similar to addition, but instead of adding both vectors, we will reverse the one that is being subtracted so that it is a negative vector and then add the two together. In the example below you can see that the subtraction of a vector reversed its coordinates so that it was a 180 degree flip and then the two vectors are added together. B-A would flip the direction of A and then add it to B. This is not commutative as you would get a different answer for B-A than A-B. B-A = (-1, 1) A-B = (1,-1).
As stated before, all vectors have both a magnitude and direction. What if we want to find the length of a vector? That is where the magnitude comes in. The equation below is also known as Pythagoras' Theorem. You will have seen it a lot if you have taken trigonometry. In the example below we calculate the magnitude of Vector A. It should look familiar as it is a 3-4-5 triangle. The magnitude of the vector A is 5.
One way of multiplying with vectors is multiplying a vector by a scalar value. This retains the previous vector direction, but increases the magnitude by some value. The vector is scale... which is why it's called scalar multiplication.
Multiplication (Cross/Dot Product)
There are two methods of multiplication that can occur with vectors. The first being the dot product, which gives you a scalar value representing the two values. The other way is the cross product which will give you a new vector. The cross product gives you a vector which forms right angles with the two vectors.
Unity has a plethora of methods that you can use to manipulate vector including dot and cross products. I've included links to the Unity documentation for Vector2, Vector3, Cross, and Dot below; simply click the topic you are interested in. To see how to apply these methods, watch my Youtube tutorial or follow the code listed below.
Vector2 Vector3 Dot Cross
Vector2 Vector3 Dot Cross
The code listed below shows a basic vector movement of a sphere. To recreate this simply put a sphere at (0,0,0) and test it out. The sphere will move to (2,1) and then move (2,1) every frame at a rate equal to time.deltaTime.
For more tutorials, follow us on Twitter and Youtube!