Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use magnitude?

Asked by 9 years ago

I know its something similar to

0,0,0.magnitude
or
1,1,1.magnitude
or
2,2,2.magnitude

I got what I know from the wiki.

1 answer

Log in to vote
4
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

magnitude is a Property of Vector2 and, more notably, Vector3 Values.

It is the 'length' of the Vector, starting at the Origin, (0, 0, 0), and you use it like so:

local vec = Vector3.new()
print(vec.magnitude) -- 0, since (0, 0, 0) is 0 studs from (0, 0, 0)

vec = Vector3.new(0, 1, 0)
print(vec.magnitude) -- 1, as vec is now 1 stud away from (0, 0, 0)

This image represents the component breakdown of a typical 3D vector. (Y and Z are flipped in comparison to ROBLOX's axes, but Z is typically height in most implementations.)

The magnitude here is the length 'a' in that image, and by the Pythagorean Theorem:

a^2 = x^2 + y^2 + z^2


The way most people use magnitude is to find the distance between two part, which is done like so:

local partDistance = (part1.Position - part2.Position).magnitude

This, obviously, calculates the length of the difference between the two Vector3 Positions. In other words, the 'length' of the Vector of displacement between the two parts, which is the distance between them.

Ad

Answer this question