How to get the middle of a target part

I need a calculation to get the position of a part (to align to the middle of a target)

This is what I want

This is how far I got

This is the code that I’m using

local function getPlacementPosition()
    
    local target, hit, normal = mouse.Target, mouse.Hit, mouse.TargetSurface
    local snappedNormal = target.CFrame:VectorToObjectSpace(normal)
    
    local cframe = (
        CFrame.new(
            hit.X,
            hit.Y,
            hit.Z
        ) * CFrame.Angles(0, math.rad(target.Orientation.Y), 0)
    ) * CFrame.new(
        ((model.PrimaryPart.Size.X / 2) + (target.Size.X / 2)) * -snappedNormal.X,
        0,
        0
    )
    
   return cframe
    
end
2 Likes

I just tried with something but seems to need more offset

Are you trying to make part A go perfectly to the middle of part B?

Yeah, I’m trying to do that with code as I’m creating a wall object placement system for doors and windows.

Try making CFrame of Part A (part to place in the middle) equal to CFrame of Part B (mouse.Target)

Yeah but I only want an axis in the middle so the player can move horizontally the window/door
EDIT: I also tried that, the problem comes when the CFrame it’s rotated.

Nevermind, I fixed it adding only the half of size X as offset CFrame in the operation.