Set part's CFrame based off of 2 attachments

Ok, this is a bit of a tricky question to explain, but I will try my best to explain it.

I’m trying to make a building system where these 2 attachments line up and snap together if in close enough proximity

local Debounce = false

local Orientation = BuildPreview.PrimaryPart.CFrame - BuildPreview.PrimaryPart.Position
		
BuildPreview:SetPrimaryPartCFrame(Orientation + Mouse.Hit.Position)
		
for _,CurrentAttachment in pairs(BuildPreview.BoundingBox:GetChildren()) do
	for _,OtherAttachment in pairs(game:GetService("CollectionService"):GetTagged("BuildAttachment")) do
		if CurrentAttachment.Parent ~= OtherAttachment.Parent and OtherAttachment:IsDescendantOf(game.Workspace) then
			if (CurrentAttachment.WorldPosition - OtherAttachment.WorldPosition).Magnitude < 1 and Debounce == false then
						
				Debounce = true
						
				BuildPreview:SetPrimaryPartCFrame(CFrame.new(OtherAttachment.WorldPosition))
			end
		end
	end
end

This script does this:

But I want the two attachments to line up. Instead what I have right now is just setting the builds position to the other attachments position

try looking at weld maths for attachment snapping

part1.CFrame * C1 == Part0.CFrame * C0
--C1 and C0 are CFrame offsets, the same as attachments CFrame as attachments CFrame is relative to the parent
part1.CFrame * part1.Attachment.CFrame = Part0.CFrame * part0.Attachment.CFrame 
--Solve for part1 which is the one thats being dragged
part1.CFrame = Part0.CFrame * part0.Attachment.CFrame*part1.Attachment.CFrame:Inverse()
--Up to you to find the two attachments closest to each other

AllignPosition and others of the sort will not work because the parts need to be anchored, and with the equasion that you provided to me, I can’t use CFrames for reasons. Is there any way I could calculate it with just Vector3’s?

Why not use CFrames? I see the code up there already uses CFrames.

I believe It can be adjust it like this:

local part1FormulaCF = OtherAttachment.Parent.CFrame*OtherAttachment.CFrame*CurrentAttachment.CFrame:Inverse()
BuildPreview:SetPrimaryPartCFrame(part1FormulaCF)

1 Like

This does not work. It just sets the BuildPreview’s CFrame to the parts CFrame

local wallCframe = RaycastInstance.CFrame *CFrame.new(attachment.Position)
wallCframe = wallCframe *CFrame.new(-ClonedSample.Attachment.Position)
wallCframe = wallCframe *CFrame.Angles(0, math.rad(attachment.Orientation.Y), 0)
ClonedSample.CFrame = wallCframe