Working with vectors
In game development, we use vectors to define directions and positions. As shown in the following figure, we draw a line between two points to represent a vector. In this case, the vector starts from the origin, which is point B (0, 0) on the graph, to point A (6, 2):
We can see this vector is made up of two components, namely x and y. They represent the distance from the origin along the x axis and the y axis. Therefore, this vector can be used to define the position of point A relative to the origin in the space. In addition to the position of point A, we can also calculate the length of the distance between these two points, and we call it the magnitude. The magnitude of a 2D vector is the square root of (x*x+y*y)
.
In Unity, we will use the Vector2 structure to represent 2D vectors and points. The magnitude property of Vector2 returns the value of the magnitude of this 2D vector.
3D vectors are...