Unity Vectors 101: Learn the Basics of Direction and Distance

Objective: Vector vs Position, Translating Vectors and Direction and Distance.

Josh P (Pixel Grim)
Unity Coder Corner
4 min readNov 22, 2023

--

Vector vs Position (A Point in Space)

In summary, a vector represents a direction and magnitude (like a displacement or movement), whereas a point represents a location in space. This means that two vectors with the same direction and magnitude, such as V1 <2,3,0> and V2 <2,3,0>, can exist independently of each other by having different starting and ending points. For example, when adding these vectors to different starting points, they both result in different ending positions despite having the same vector.

Unity: Vector Translate

Translating a vector in Unity is similar to the above example we have a vector (Direction and Magnitude) and we add it to an object's current position to get a new position. Below I have two spheres one sitting at the origin and another one at a random spot in space. Within the move script, I translate the spheres with a new vector3 (2,0,3). As you can see both move in the same direction but their endpoints are different.

Find the Direction Vector of Another Object

Now, instead of passing in a random direction, let’s calculate the direction vector from one object to another. This can simply be done by subtracting the target’s position from the player’s position. As you can see below both spheres will calculate the direction they need to travel and teleport to that position!

  1. Calculate the direction vector
  2. Then translate the “player” object with the calculated direction.

Finding the Distance Between Objects

Since we’ve calculated the direction between the two objects, we now have the vector we need. We can use this direction vector to find the distance! This is possible because, as stated above, vectors represent both direction and magnitude. The term ‘magnitude’ also refers to length or distance. We can easily find the distance by using the following method: direction.magnitude. This will give us a float value representing the distance between the two objects and the length of the vector.”

  1. Within the movement script, I calculate the direction between the spheres and the goal post.
  2. Given the direction vector, I find the magnitude (Distance) Value
  3. If the sphere distance is less than 10 then move the sphere to the goalpost!

In conclusion, we have discussed the concepts of vector and position in Unity, and how they can be used to manipulate the movement and position of game objects. We have also learned how to use the Unity Vector Translate method to move an object along a given vector, and how to find the direction vector and the distance between two objects using simple methods. To summarize, here are the main points I have covered:

  • A vector is a quantity that has both magnitude and direction.
  • A position is a point in space that defines the location of an object, and it can be represented by a vector from the origin to the point.
  • Unity Vector Translate is a method that moves an object along a given vector by adding the vector to the object’s position vector.
  • The direction vector of two objects is the vector that points from one object to another, and it can be found by subtracting the position vector of the second object from the position vector of the first object.
  • The distance between two objects is the length of the direction vector, and it can be found by using the magnitude function.

--

--