I want find the lenght of 2 Vector. But it seems like that magnitude and pythagorean theorem gives different solutions.
local Part1 = game.Workspace.Part1
local Part2 = game.Workspace.Part2
local function Pythagora(Va)
local lenght = math.sqrt(Va.X^2 + Va.Y^2 + Va.Z^2)
return lenght
end
local Part1 = Pythagora(Part1.Position)
local Part2 = Pythagora(Part2.Position)
local lenght = Part2 + Part1
local magnitude = (workspace.Part2.Position - workspace.Part1.Position).Magnitude
print(magnitude)
print(lenght)
The reason you’re function isn’t working is you’re trying to find the length of the vector from 0,0,0 on two different parts and then adding those lengths together. You forgot to subtract one vector from the other vector before doing Pythagoras Theorem.
function distance(vector1,vector2)
local difference = vector2 - vector1
local distance = math.sqrt(difference.X^2 + difference.Y^2 + difference.Z^2)
return distance
end
It does not work because here you’re not calculating the same thing as here:
With magnitude you’re calculating the vector from part1 to part2 and with pythagorean you’re calculating the sum of part1.position vector length + part2.position vector length.
In order to get the same results you can do for example:
Pythagora(Part2.Position - Part1.Position)
or
local Part1 = Part1.Position.Magnitude
local Part2 = Part2.Position.Magnitude
local lenght = Part2 + Part1