Get Distance from surfaces

I want to know the distance in studs of two parts, but not from the center, but their faces.

Using the method of subtracting both positions and the magnitude of the result, it gives me what I don’t want:

Anotaci%C3%B3n%202019-11-03%20015810

If I change the orientation in any way it will give me exactly the same:

Anotaci%C3%B3n%202019-11-03%20015935

I know why, even so, what I want is to get the distance like this:

2

With so many images I hope you understand what I mean. Maybe the solution is something related to Rays or Regions, but so far I can’t find one.

If possible, I need a way to do it without specifying a face, that works at any angle.

2 Likes

try calculating the distance from attachments

1 Like

You can easily get the CFrame and Position of the Surfaces of the part by using this method:

local Part1 = ...
local Part2 = ...

function GetSurfaceCFrames(Part)
	local Positions = {}
	
	Positions["Top"] = Part.CFrame * CFrame.new(0, Part.Size.Y/2 ,0)
	Positions["Botton"] = Part.CFrame * CFrame.new(0, -Part.Size.Y/2 ,0)
	Positions["Right"] = Part.CFrame * CFrame.new(Part.Size.X/2, 0, 0)
	Positions["Left"] = Part.CFrame * CFrame.new(-Part.Size.X/2, 0, 0)
	Positions["Front"] = Part.CFrame * CFrame.new(0, 0, -Part.Size.Z/2)
	Positions["Back"] = Part.CFrame * CFrame.new(0, 0, Part.Size.Z/2)
	
	return Positions
end

local Part1SurfaceCFrames = GetSurfaceCFrames(Part1)
local Part2SurfaceCFrames = GetSurfaceCFrames(Part2)

and you can compare them by the Position of it’s CFrames:

print((Part1SurfaceCFrames.Top.Position - Part2SurfaceCFrames.Right.Position).magnitude)
6 Likes

Try this code:

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
3 Likes

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:

https://gyazo.com/85a822a536d164d85caf2e35ba7120a8

Why did you make the question if you knew there were alternative ways to script distance from surfaces?

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.

7 Likes

alright im sorry i wont say that again :cry:

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.

3 Likes

I found the solution long after asking the question …

1 Like

Hello, how exactly did you do this? Where are the rays being shot from?

How were you able to do this? Could you possibly sende any code?

1 Like