I want use lengh instead of magnitude but it seems not working:
local Part2 = script.Parent
local Part1 = game.Workspace.Part1
local pa
local function Lengh(Vector1, Vector2)
local a = math.sqrt(Vector2*Vector2+Vector1*Vector1)
return a
end
local d = Lengh(Part1, Part2)
print(d)
Error: Workspace.Part2.Script:5: attempt to perform arithmetic (mul) on Instance - Server - Script:5
Did you ever put the positions pf the parts in this script? It seems like you are only putting the parts instance other than the parts position/CFrame.
local Part2 = script.Parent
local Part1 = game.Workspace.Part1
local function Lenght(Vector1, Vector2)
local a = math.sqrt(Vector2*Vector2+Vector1*Vector1)
return a
end
local d = Lenght(Part1.Position, Part2.Position)
print(d)
Why do i get different values bot get the differences
local Part2 = script.Parent
local Part1 = game.Workspace.Part1
local function Lenght(Vector1, Vector2)
local a = Vector1.X^2 + Vector1.Y^2 + Vector1.Z^2
local b = Vector2.X^2 + Vector2.Y^2 + Vector1.Z^2
local result = math.abs(a + b)
return math.sqrt(result)
end
local magnitude = (Part2.Position-Part1.Position).Magnitude
local d = Lenght(Part1.Position, Part2.Position)
print(magnitude)
print(d)
I think you are not supposed to use addition when calculate the length:
local function Length(Vector1, Vector2)
local a = Vector1.X^2 + Vector1.Y^2 + Vector1.Z^2
local b = Vector2.X^2 + Vector2.Y^2 + Vector1.Z^2
local result = math.abs(a - b)
return math.sqrt(result)
end
Oh and additionally, the calculation is in wrong order, perhaps this one is the right one?
local function Length(Vector1, Vector2)
local x, y, z = (Vector1.X - Vector2.X)^2, (Vector1.Y - Vector2.Y)^2, (Vector1.Z - Vector2.Z)^2
local result = x + y + z
return math.sqrt(result)
end
I updated the post above again and apparently when I looked a bit closer into vector math, I remembered that you should calculate the difference between their respective vectors; X for X, Y for Y and Z for Z. Then you can use Pythagorean theorem. Calculating their differences results in a space where either Vector3 becomes the origin in theory.