I"ve created a function that returns the range between 2 parts from their Position. Considering how long it is, I immediately smelled spaghetti, and am wondering what the correct way of finding the range between 2 objects is?
Here is the code in question:
local function findRange(origin, destination)
if typeof(origin) == "Vector3" and typeof(destination) == "Vector3" then
--first we abs everything
origin = Vector3.new(math.abs(origin.X), math.abs(origin.Y) ,math.abs(origin.Z))
destination = Vector3.new(math.abs(destination.X), math.abs(destination.Y) ,math.abs(destination.Z))
--subtract to acquire range
local range = origin - destination
range = Vector3.new(math.abs(range.X), math.abs(range.Y), math.abs(range.Z))
--solve right triangle with 2 sides(X and Z), no angles
local angle_K = math.atan(math.tan(range.X, range.Z))
local magnitude_2D = range.X / math.cos(angle_K)
--solve ANOTHER right triangle with 2 sides(final_range and Y), no angles
local angle_V = math.atan(math.tan(range.Y, magnitude_2D))
local magnitude_final = magnitude_2D / math.cos(angle_V)
--now we have absolutely and undoubtedly found range in the most effective way possible :)))))
return magnitude_final
end
end