What's the difference between .Unit and .Magnitude?

For some time I’ve been trying the “mathematical” part of roblox scripting and stumbled up a part where some users use .Unit and .Magnitude. Well, I do know what magnitude is, it calculates the distance between 2 real-time points like → (pos1 - pos2).Magnitude but I haven’t seen an explanation for .Unit yet, maybe you will be the first one?

3 Likes

The Unit vector is any vector whose Magnitude is 1. In other words, it normalizes a vector so that its length will be equal to 1. Useful for certain applications which I will not delve in to.

The Magnitude of a vector is its length given by the formula sqrt(x^2 + y^2 + z^2).

2 Likes

So, Vector.Unit is basically Vector / Vector.Magnitude?

4 Likes

So you mean the unit vector is a constant and not a multivalue like magnitude? Where .Unit will remain only 1 wherever you use it?

1 Like

It’s not constant. It’s the vector divided by the magnitude. Remember that vectors are made of two theoretical components. Length/distance/magnitude, and direction. Unit is the direction component without any magnitude. You’ll see it quite often in raycasting, where they do direction.Unit*distance. They’re basically combining the direction component with a new magnitude component.

2 Likes

Yup! The formula for normalizing a Vector is v / ||v||, where v is your Vector and ||v|| is the magnitude of that Vector.

2 Likes

You can think of a vector as just data. You can use the data in different ways. Usually vectors are treated as either positions in space, or as a direction with a distance.

It is easy to understand how Unit and Magnitude work when you treat vectors like a direction.

direction = (pos2 - pos1)

-- This is no longer a vector. It is just a number representing the length
distance = direction.magnitude

-- This is the direction with a specific length of 1
dirNormalized = direction.unit
2 Likes

So basically, the direction it is facing? Correct me if I’m wrong.

3 Likes

“direction” in the example I posted, is a vector that represents how to get from pos1 to pos2. It is a direction with a distance built in. If you want strictly the direction, with no distance involved, you would use Unit. Unitizing a vector will keep the orientation of the vector but make it’s length equal to 1.

Unitizing a vector is useful for cases such as character movement. You can convert player input into a vector, then unitize it so that it is just the direction with a constant length. Then you can multiply the unitized vector by how fast you want the character to be moving. If you don’t unitize the input vector, the character may move faster or slower than expected when you multiply the direction by the speed.

2 Likes

Ok I completely understood you but what does the length of 1 mean?

Sorry for late response…

1 Like

It means the magnitude of the vector is 1. That also means, the distance from the origin is 1. A unit vector always has a length of 1.

There are plenty of answers here, you should mark the most informative one as the solution.

1 Like

You are Diving the Vector by its Distance from the Origin, Normally when you divide a number, if you have less than the number you are dividing by, it would be less than one, for example: 5/100 which is equal to .05, or 21/365 which is equal to .057534246575342465, If you can tell, they are all very small numbers, because in the of 5/100, you are trying to split it into 100 pieces, with .05 being the result of that, if you divide a number by itself, like 250/250, it will give you 1, because 250 fits into 250 only once.

For the Vector3 Value, lets say you have 20, 40, 60, to get the Length of that, you need to use whats called Pythagorean Theorem, There are Muliple Versions of the Pythagorean Theorem, for 2D spaces, the Forumla is (a² + b² = c²), but in 3D spaces its (a² + b² + c² = d²), you then need to find the square root of the number given, and the result is your length.
Normalizing Happens when you Divide the Vector by the Length, For our Example stated earlier, that length of that would be roughly 74.83 (Rounded), If you want to be exact: 74.83314514160156
The Length will always be bigger than the Vector, because when using Pythagorean Theorem, you are basically trying to get the number of c (The Hypotenuse), which is always the biggest side of the Triangle when using Trigonometry (assuming this is Trigonometry, I dont remember), so when you divide one of the axis by the length, you would give you a number in between 0 and 1.

Example:
20/74.83314514160156 = 0.2672612511762721
40/74.83314514160156 = 0.5345225023525442
60/74.83314514160156 = 0.8017837535288163

As they are smaller than 74, less than 1 group will fit into the number.
So these 3 numbers would be the Normalized Version of your Old Vector, or Vector3.Unit

(May be wrong)

3 Likes

I think it might be helpful to simplify it to a single dimension. Let’s make a Vector1 with only an X axis. Like a number line.
There are only two possible directions: positive X, and negative X.
The “length” is how far the number is from 0. If the Vector1’s X value is 10, then the vector has a magnitude of 10 or a length of 10. If the Vector1’s X value is -0.5, then the magnitude is 0.5. Not negative. The magnitude is a distance, and it doesn’t care about direction.

A unit vector (or “direction”) is a vector with a magnitude of 1. So all we need to do is divide the vector by the magnitude to get the unit vector.
If the vector’s X axis is 10, then the magnitude is 10. 10 / 10 = 1.
If the vector’s X axis is -0.5, then the magnitude is 0.5. -0.5 / 0,5 = -1
From that, you can see how the unit vector illustrates direction. We can multiply the unit vector to get a vector of a consistent length. If we want our vector to have a magnitude of 5, we multiply the unit vector by 5.
1 * 5 = 5 to get a vector of length 5 in the positive direction.
-1 * 5 = -5 to get a vector of length 5 in the negative direction.

This concept scales well to any number of axes. A Vector2 has two axes, but infinite possible directions. The reason is that the true direction could be some number of X direction and some number of Y direction. It could be purely axial, such as X, Y = 1, 0. Or it could be a ratio between X and Y. X, Y = 0.707, -0.707 is 45 degrees in the positive X, negative Y direction because the the absolute value of X and Y are equal. If the absolute value of Y is more than X, the angle becomes more vertical.

Magnitude in two dimensions is calculated by the Pythagorean theorem: math.sqrt(x^2 + y^2). When 0.707, -0.707 is fed into the Pythagorean theorem, you will see that it is indeed a unit vector with a length of 1. We can multiply this 45 degree direction by any number to get a vector of a certain magnitude in that direction.

This scales up to a Vector3 in the same way. The only difference is when calculating magnitude. The Pythagorean theorem is now used with 3 components: math.sqrt(x^2 + y^2 + z^2).

8 Likes

Wow, you guys have given me the best explanations I could ask for…

Thank you, @Negativize, @bluebxrrybot, @JarodOfOrbiter, @VitalWinter and @DasKairo for helping me out for this problem, you guys are amazing!

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.