local function getPositionFromFace(face, part)
local c = part.CFrame
local rv = c.RightVector
local uv = c.UpVector
local lv = c.LookVector
local s = part.Size / 2
local rightFacePos = Vector3.new(0,rv * s.X,0)
local leftFacePos = Vector3.new(0, rv * -s.X)
local topFacePos = Vector3.new(0, uv * s.Y,0)
local bottomFacePos = Vector3.new(0, uv * -s.Y,0)
local frontFacePos = Vector3.new(0, uv * s.Z,0)
local backFacePos = Vector3.new(0, uv * -s.Z,0)
if face == "Front" then return frontFacePos end if face == "Back" then return backFacePos end if face == "Right" then return rightFacePos end if face == "Left" then return leftFacePos end if face == "Top" then return topFacePos end if face == "Bottom" then return bottomFacePos end
end
local distance = (getPositionFromFace("Front", p1) - getPositionFromFace("Right", p2)).magnitude
print(distance) --its in studs
Thanks for all the answers, but I have found for myself a more efficient way in which I can do it without having to specify any faces, simply using two rays from each part:
Questions can be asked to gather information, ideas or methods as well as get full solutions to problems.
By asking a question here, if your aim is just to get code that works and be done with it, then you’re not really going to expand your knowledge. Having code that works is one thing, understanding it and being able to identify its efficiency is another. It’s great to have multiple answers to a question, even if they’re all different - because that’s one of the great things about programming, there’s so many ways to solve problems!
Having multiple answers and seeing other people’s suggestions allows people to compare them and identify their strenghts and weaknesses. From that, people can develop better solutions and everyone can learn from it.
Even if you asked the question, you should always be trying to seek an answer yourself. You may see responses that spark an idea in your head, leading you to a solution that you can develop yourself or maybe just discuss. Ultimately, answering your own question isn’t bad at all - especially when you share your solution for others to see. This way, everyone can learn.
Nothing was directed at you there, no need to be sorry. It’s just great to have contribution, whether that’s from the OP or someone else. We’re a community of developers, and we solve things together, and any contribution could spark an idea for someone else who ends up with the final solution.