How to work out CFrame movements? (Recreate the distance between two objects, and orientation)

I have Part A and Part B.

Part A - Red
Part B - Green
image

This is part C:
image

I want to add another Part (D) so that it is in the same position as B is relative to A.
(Regardless of orientation, etc).

EDIT: I know I can work out the vector3 distance and apply, but if the part changes orientation it wont work. Also, if the part is facing between two directions it wont work.

How would I go about doing this?

If im understanding what you want correctly then it should just be a positional offset, which should work regardless of rotation.

local PartA: BasePart = (...)
local PartB: BasePart = (...)
local PartD: BasePart = (...)

-- similiar to raycasts, (finalpos - initalpos) = position relative to initalpos
local RelativePosition = PartB.Position - PartA.Position 

--then we can just add back part A's position (or any other part that you want the relative position to be on) and apply the relative position on top of it.
PartD.Position = PartA.Position + RelativePosition
1 Like

This method has the following outcome:

image

For context, this is going to be used for a camera and character - so they need to be facing / rotated exactly as parta and b are. If PartA is rotated so it’s behind itself PartB should be projected in that direction too.

So, you want the part to keep the rotation, but be the relative position away based on Part C’s rotation?

local PartA: BasePart = (...)
local PartB: BasePart = (...)
local PartC: BasePart = (...)
local PartD: BasePart = (...)

local RelativePosition = PartB.Position - PartA.Position

PartD.CFrame = PartC.CFrame * CFrame.new(RelativePosition) * PartB.CFrame.Rotation

Just use :ToObjectSpace(), would be much simpler:

local RelativePos = PartB.CFrame:ToObjectSpace(PartA.CFrame)

PartD.CFrame = PartC.CFrame * RelativePos