What is Vector3?
When a GameObject moves due to physics calculations or simple position changes, its position value changes.
This position is represented by a structure called Vector3.
As shown in the images above, the Transform's position is a Vector3, and you can see that Vector3 is a struct.
So, what is Vector3 used for?
Position
As the name suggests, it is used to determine an object's position.
Vector3 operates based on three axes:
- x-axis: horizontal
- y-axis: vertical
- z-axis: vertical in the 2D space relative to the x-axis
This is how it works.
In 2D space, the z-axis is typically interpreted as the depth (front-back positioning) of an object.
Direction
In 2D vectors, direction can be easily calculated using the Pythagorean theorem.
Let’s say User A is at position (1, 2), and User B is at position (4, 8).
Using the equation , you can find the direction from A to B.
However, determining direction becomes more complex in 3D vectors.
So, it’s helpful to know that direction can be derived using vectors
Vector Magnitude
The segment h that connects the positions and direction represents the vector’s magnitude.
While it's simple to calculate in 2D,
in 3D space, calculating the magnitude is more challenging due to the additional axis.
This is where magnitude comes in handy.
By computing the difference between two points in 3D space and using the magnitude,
you can find the length of the vector between them.
Vector3 A = new Vector3(4, 8, 7)
Vector3 B = new Vector3(3, 10, 5)
float magnitude = (A - B).magnitude
It can also be used to calculate speed:
Vector3 velo = new Vectore(1, 2, 3);
float magVelo = velo.magnitude;
The magnitude of a vector is used to measure distance, calculate speed, and determine direction.
The magnitude is calculated as .
sqrMagnitude gives the value before the square root, i.e., .
When you only need to compare distances, you can use sqrMagnitude to avoid the computational cost of a square root operation.
Vector3.magnitude
normalized
normalized is used when the magnitude (distance or speed) varies and you want to normalize the vector’s size.
Vector3.noramlized
Its implementation looks like this:
A normalized vector has a magnitude of 1 but retains its original direction.
It’s the result of dividing each component of the Vector3 by its magnitude.
For example, if Vector3 = (2, 2, 1), the magnitude is 3, so
it becomes (2/3, 2/3, 1/3).
Then, calculating the magnitude:
— it has a length of 1.
using UnityEngine;
public class Test : MonoBehaviour
{
private Vector3 point = new Vector3(1, 0, 0);
private Vector3 diagonalPoint = new Vector3(2, 2, 1);
void Start()
{
Debug.Log($"point: {point.magnitude}");
Debug.Log($"diagonalPoint: {diagonalPoint.magnitude}");
Debug.Log($"normalized Point: {point.normalized.magnitude}");
Debug.Log($"normalized DiagonalPoint: {diagonalPoint.normalized.magnitude}");
}
}
If you run the above code to test normalized,
you’ll get the following result:
Let’s assume a user jumps in a 2D space.
If they jump from coordinate (0, 0) to (1, 1),
the horizontal movement is 1 unit on the x-axis,
but the jump’s force, using the Pythagorean theorem, is = 1.414213...
Therefore, because each axis’s velocity differs, using normalized adjusts the direction vector to a magnitude of 1,
allowing consistent force or speed calculations.
Top comments (0)