Wow, okay so I tried to complete your function: (tell me if this is correct)
local function hisDist(part1: BasePart, part2: BasePart): number
local size1, size2 = part1.Size/2, part2.Size/2
local cframe2 = part1.CFrame:Inverse() * part2.CFrame
local x, y, z = math.abs(cframe2.X) - (size2.X + size1.X),
math.abs(cframe2.Y) - (size2.Y + size1.Y),
math.abs(cframe2.Z) - (size2.Z + size1.Z)
return math.sqrt(math.max(x, 0)^2 + math.max(y, 0)^2 + math.max(z, 0)^2)
end
and then I made this:
local start = os.clock()
print(generalDist(part1, part2))
print("Time: ", os.clock() - start)
start = os.clock()
print(myDist(part1, part2))
print("Time: ", os.clock() - start)
start = os.clock()
print(hisDist(part1, part2))
print("Time: ", os.clock() - start)
where generaldist is the (pos2 - pos1).Magnitude, mydist is my function and hisdist is your function.
Your function returns ~12.46, mine returns ~13.84 and the general distance returns ~14.71.
Yours seems to be the most accurate, but for some reason it is also the slowest, I ran this script 100 times and general distance is 0.0001s slower than mine, yours is 0.0001/0.0002s slower than mine, but I’m assuming yours is also the most optimal because I haven’t checked how much memory raycasting uses, if you were to run this every tick in like .HeartBeat, I’m just assuming yours would be optimal there. The difference is also very small.
So yes you’re completely right, haven’t thought of that.