What is .magnitude

can somebody explain .Magnitude in simple terms. Ex:
local Distance = (script.Parent.CFrame.p - Position).Magnitude

I get that it’s the distance between the origin and result, but I don’t understand what making this equation really MEANS.

3 Likes

You just defined what the equation really means, it gets the distance between origin and result

1 Like

When you subtract two vectors the difference is the X, Y distance between the two, just like how if I subtract 5 from 8 I get 3, the distance from 5 to 8. However the X, Y distance is two components and expressed as if one moves along the X axis → then rises on the Y axis ↑ like a triangle. Normally we want the closest distance in a straight line. To retrieve this we use the pythagorean theorem, where X is a and Y is b leaving our mystery distance-as-a-number to be c the hypotenuse.

image

A vector representing distance from origin, with a dotted line along the axis to form a triangle

Since this is such a common function Roblox has optimized it for us in the form of Vector3.Magnitude.

In vector mathematics it is also called the length of the vector. We can also extract the direction of the vector with Vector3.Unit.

23 Likes

this is extremely helpful. thanks a bunch.

1 Like

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