I’ve recently been experimenting around with things like raycast, and I’ve stumbled apon “.Unit” and “Magnitude”, in which most people explained it using IRL physics and more complex code which I was too inexperienced to comprehend.
However, through these posts I think i’ve gotten the gist of it. so now i’m just looking for clarification and such; I provided an painting below of what I think these terms mean.
If i’m not wrong, .Unit gives a direction changable length, and magnitude measures it right?
In simplest terms, a unit is a normalized vector, which has a length of 1.
I guess a good way of visualizing the difference would be with the following code:
local direction1 = somePart.CFrame.lookVector * 100
local direction2 = somePart.CFrame.lookVector.Unit * 100
If you set a part’s velocity to each variable above, you should notice a huge difference between the two. The second one should be more stable, the reason for this is that we basically reduce the length of the lookVector to 1, and then multiply that by the length that we want, contrary to just multiplying the already large vector by 100.
A Vector3’s magnitude is pretty much the length of a vector in a number.
You can subtract two vectors and get the magnitude which return a number in units to compare the distance between them both.
Let’s say we have two vectors,
local vector1 = Vector3.new(-6, 0, 0)
local vector2 = Vector3.new(6, 0, 0)
One is six studs apart of 0 towards the left and another one is six studs apart from 0 to the right. The length of both vectors is 6 no matter what, but the distance between these is 12 studs apart. It completely removes the negative factor of those coordinates.
print((vector1 - vector2).magnitude) --> Prints 12
print((vector1.magnitude - vector2.magnitude)) -- Prints 0
The first option (vector1 - vector2).magnitude is the correct way to compare two Vector3’s and get the length of it.
Facinating! After seeing your post I looked up some of the terms you used and now understand it. The last example you gave me was also pretty helpful in getting me thinking of some of the uses of it too.