Vectoring to Success

My girlfriend is a physicist and today she told me she loved me to the moon and back

She then proceeded to inform me that love is a vector.

This will be a short post but comes after I faced what was the hardest problem I have ran into in Unity to date.

But this post is not about that problem (that will be in a future post). This post is about what helped me solve the problem: Vectors.

When you start coding in Unity and need to position and object you quickly learn to use a Vector3 (or Vector2 without the z) and it is quickly apparent that a Vector is 3 points x, y and z representing the point in space you want your object. But if you are like me you begin to wonder why is it called Vector3 and not Point3 after all you are using it to position an object at a point in space.

The answer goes back to what a Vector is. Don't worry I will provide some helpful links after I am done rambling! Basically a vector has two components direction and magnitude. In other words which angle is the vector pointing to and how long the vector is. But then how can you represent it using only three components x, y and z?

For most situation we encounter it turns out setting one end of the Vector to 0,0,0 works just as well as setting it to a position away from the origin. The vector (0,0,0 to 1,1,1) is the same as (1,1,1 to 2,2,2) but in the first case we only have to store 3 bits of information not 6. There are apps online or drawing vectors on graph paper can let you see this visually.

Because vectors can convoy direction as well as magnitude they are useful for a wide array of things such as finding out where an enemy is in relation to the player, determining the forward velocity of a car while in a skid, adding force to objects and more.

Two pages were useful for me when I was trying to learn more about Vectors in game design:

The first is Unity's own page explaining Vector math in games and the second describing what a dot product is and why you should use it (Spoiler: It's faster then most other vector operations!)

At the end this post is only a brief of brief mention of Vectors and hopefully encourages you to learn more about them. They will arise in many situations in game development and understanding vectors will explain to you why we use Vector3 instead of Point3 to position objects.

In the end 20+ hours of frustration and trial was solved with ONE line of code doing a dot product between two vectors. More on that in the future.

Comments 1