How do I CFrame a part inside a model to a CFrame while using SetPrimaryPartCFrame?

image

I want to know how I can move the model on the right using Model:SetPrimaryPartCFrame() so that CFrame2 (part of the model) is at the same position and facing the same way as CFrame1 (not part of the model).

All I know of is temporarily set the primarypart to CFrame2, then CFrame it to CFrame1, then revert the primaryPart.

local targetPart = -- put CFrame1 here (instance)
local model = -- put model here (instance)
local startPart = -- put CFrame2 here (instance)

local partOffset = startPart.CFrame:Inverse() * targetPart.CFrame
local calculatedModelOffset = model:GetPrimaryPartCFrame() * partOffset
model:SetPrimaryPartCFrame(calculatedModelOffset)

Explanation:
We have to calculate the offset between Cframe1 and Cframe2, then multiply the primary part cframe with offset. After calculating it, we can apply the cframe with SetPrimaryPartCFrame.

1 Like
local offset = CFrame2:Inverse() * model:GetPrimaryPartCFrame()
local cf = CFrame1.CFrame.Rotation * offset + CFrame2.Position
model:SetPrimaryPartCFrame(cf)

CFrame1 and CFrame2 are Parts while model is the model.

1 Like

This is what it does

This is what happens