By now you may have noticed a lot of talk in the Unity docs regarding Vectors. But what are they exactly? Is it an objects position? Does it have anything to do with images? HELP!
What is a Vector?
– Put simply: a Vector is a line drawn between two points. Not exactly the answer you were expecting, huh? Well, tough! A lot of your answers won’t be filled with magic and rainbows!
– You can’t speak about Vectors without mentioning magnitude: the length of a Vector.
That was cute…but seriously, what is a Vector?
I’m not pulling your chain. On the very base, this is what a Vector and magnitude are. Luckily for you, in relation to what you need to know for Game Development and Unity, it gets more complex:
– A 2D-Vector is a way of representing a point from the Origin point (0,0) to any point on the 2D plane. Also note: Since it is relative to the Origin point, it has an implied direction.
If you see the chart above, you’ll notice the Origin point (0,0) and the current position (12, 5). This is an example of a 2D-Vector. Just keep repeating in your head “A 2D-Vector is any point in the 2D Plane in relation to the Origin point (0,0)”, and you’ll be fine… I think.
We brought up that magnitude is the length of a Vector. more specifically, magnitude would be the length of a distance between 2 points. This is going to get real math-y, but please, bare with me. In order to find the magnitude of a 2D Vector, you can use Pythagorean’s Theorem to work it out mathematically.
The Pythagorean’s Theorem goes as follows:
If we plug in our current position in the 2D-Plane, which would be (12,5), to the Pythagorean’s Theorem, we work it out as such:
I want to make this evident: Math is not magic. Everything has an explanation. Everything is simpler than you think, and equally more complex than you give it credit for. Please, do not be intimidated. Formulas are there to help you. Math is there to help you.
What do we know so far, gents?
So, we know what a Vector and a magnitude are, in it’s simplest forms. We know what a 2D-Vector is, and we know how to find the magnitude for a 2D-Vector. It seems that we have position and length covered, but what about movement? Does this live within the domain of Vectors? You damn right it does!
What is Velocity?
Simply put, Velocity is a change in position over time.
Let’s say you had a position of (5,6), and a velocity of (12,5) per hour. What does this mean? Well for one, we know that in an hours time, you will have traveled 12 positions further in the x axis and 5 positions further in the y axis — from your current position.
From this point, we can add the provided velocity with your current position to come up with the new position:
Now we have two points: the previous position (5,6) and the new position (17,11) after the velocity is applied. The velocity stays the same, as it is not a position, but a change in position over time. Keep in mind that all Vectors, whether current positions or new ones, are relative to the origin. This applies to velocity vectors as well. In the same way that (5,6) is a position relative to (0,0), (12,5) is a velocity relative to 0 motion.
AND NOW FOR THE THIRD DIMENSION! 😀
Now, now. Don’t be a wuss. The rules don’t change much from 2D to 3D, we’re just adding another dimension to the rules (sorry…). Where 2D-Vectors work with the X and Y planes, 3D-Vectors work with the X, Y, and Z planes. The 3D-Vector is always in the order of (x,y,z). Lastly, the Z plane represents depth.
Just to show you that 3D is not so different from 2D, let’s find the magnitude for the provided 3D-Vector in the same way we did for the previous example. The current position is (12,7,5).
In order to find the magnitude of a 3D-Vector, you can use Pythagorean’s Theorem to work it out mathematically (JUST AS YOU DID FOR 2D-VECTORS :D). All you have to do is add the Z plane to the formula, and your good to go:
As game developers using Unity, we spend an obscene amount of time dealing with Vectors. Understanding Vectors turns them into something you can comprehend, instead of an abstract value you are dealing with. The scripting resource goes into deeper detail detailing 3D-Vectors, but for the purpose of understanding what a Vector is, I believe that this was the better stopping point. I thought I was going to cry reading this info, but it was surprisingly straightforward! Keep at it, everyone!