How to calculate distance between 2 CFrame values

I need to get the distance of the Z value between 2 parts

I originally tried doing

Red.CFrame.Z - Blue.CFrame.Z

but this isn’t accurate when the parts Orientation changes

image

like in the image I want Black to be in front of Blue

but by doing
Black.CFrame = Red.CFrame*CFrame.new(0,0,distance)

2 Likes

Subtract the positions and find the magnitude.

Code:

local distance = (Red.Position - Blue.Position).Magnitude

This solution can work too

local distance = math.abs(Red.Position.Z - Blue.Position.Z)
1 Like

this wouldn’t work if I rotate all the parts cause then black part would go in a different direction

Then you would need trigonometry

I can help you with that, just tell me what kind of rotation you want to get

1 Like

You need to use dotproduct.

local CFrameA = -- CFA
local CFrameB = -- CFB
local Dot = math.acos(CFrameA.LookVector:Dot(CFrameB.LookVector))
print(math.rad(Dot)) -- prints orientation related difference in angles.
-- For Positional:
local Distance = (CFrameA.Position-CFrameB.Position).Magnitude
print(Distance)

I’m not sure on how to do that but basically what I need it is
image
the black part and blue part would be positioned correctly here

but if I move the blue part to the side I want the black part to stay in front of it

but by positioning it at the red parts position + the distance

image

to always be in front

by using CFrame also

I also want to know all the X Y Z distance between the parts

2 Likes
-- Defines the parts
local Red = workspace.Red
local Black = workspace.Black
local Blue = workspace.Blue

-- Loops
while true do
	-- Gets the positions X and Z from each part
	local X = Blue.Position.X
	local Z = Red.Position.Z
	
	-- Sets the position X and Z to the black position
	Black.Position = Vector3.new(X,0.5,Z)
	
	-- Waits a little bit for you pc not exploding
	wait()
end

1 Like

the only thing is when rotating the parts all together then black position isn’t the same anymore

¿Could you give me more explanations of how you want to it to work?

Oh I see what you are trying to accomplish!

local red = -- red part
local black = -- black part
local blue = -- blue part

local z = Red.Position.Z

blue.Changed:Connect(function()
	local x,y = blue.Position.X,black.Position.Y
	black.Position = Vector3.new(x,y,z)
end)

For @JESUS5PACHECO Using while loops can cause a lot of ping issues, its better to use .Changed Events.

2 Likes

Ah I see what your trying to say. Please wait a bit while I code it.

1 Like

so what u showed me was close to what i wanted but
if you select all the parts and did ctrl+r
and have that script run
the black part will be positioned differently and not the same before you rotated it

1 Like

¿Could you explain what is exactly what you want?, its hard to tell with only those images

OP wants the part to stay attach no matter how they are oriented.

1 Like

in your video the Black part was on the left of the Red part

I want it to always stay on the side its on even if you rotate the parts

The positions are changed also, not only the rotations, so this problem will be a lot more complex of solving

Could you explain more clearly how you want the parts to “stay on the side”

1 Like


OP wants this ^^^

1 Like

image
you see here the Black part is on the left of the Red part

image
then here I rotated it but it stays on the left side of the red part but always in front of the blue part

1 Like