DEV Community

Cover image for Vector3 from Unity
JaeHonity
JaeHonity

Posted on

Vector3 from Unity

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.

Vector3_1

Vector3_2

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

xyz

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).

Pythagorean_1

Using the equation f2+g2=h2f^2 + g^2 = h^2 , you can find the direction from A to B.

However, determining direction becomes more complex in 3D vectors.

blabla

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.
A,B=<4,8>\overrightarrow{A,B} = <4,8>

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
Enter fullscreen mode Exit fullscreen mode

It can also be used to calculate speed:

Vector3 velo = new Vectore(1, 2, 3);
float magVelo = velo.magnitude;
Enter fullscreen mode Exit fullscreen mode

Vector3.magnitude

The magnitude of a vector is used to measure distance, calculate speed, and determine direction.

The magnitude is calculated as x2+y2+z2\sqrt{x^2 + y^2 + z^2} .
sqrMagnitude gives the value before the square root, i.e., x2+y2+z2x^2 + y^2 + z^2 .
When you only need to compare distances, you can use sqrMagnitude to avoid the computational cost of a square root operation.
magnitude
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:

normalized_1

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:
(23)2+(23)2+(13)2=1\sqrt{(\frac{2}{3})^2 + (\frac{2}{3})^2 + (\frac{1}{3})^2} = 1 — 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}");  
    }  
}
Enter fullscreen mode Exit fullscreen mode

If you run the above code to test normalized,
you’ll get the following result:

normalized_2

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 12+12\sqrt{1^2 + 1^2} = 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)