Position part with math

Hi, I’m working on a game and need to place the part on top of another part even if it’s with angle.


Need to place part 2 (top one) on the blue X I draw there.
I set part2’s Y position to be half of part2 Y size + half of part1 Y size + part1 Y position, but I got the same thing as in the picture. I need to move it down and right like the red arrow is pointing to so they would be aligned.


This is how it looks like when I rotate them together.
I can’t calculate the length from part2’s pivot to the X mark. I’ve tried to move it by half of part2’s X size and a few more like sizeX/3, sizeX/4, sizeX/6 and sizeX/9 (sizeX/math.huge = 0 so it’s useless).

part2.Position = Vector3.new(
    part1.Position.X + ???,
    part1.Position.Y + part1.Size.Y/2 + part2.Size.Y/2, -- This works perfectly if parts are exactly 90 degrees on the baseplate.
    part1.Position.Z, -- I'm not sure if I'll need to fix even Z, but size X is the same as size Z.
)
-- Note: Both parts must be the same rotated

Thank you for your response.

1 Like
--create two parts p1 and p2
local p1 = Instance.new("Part")
--some color for clarity
p1.Color = Color3.fromRGB(255, 170, 0)


local p2 = Instance.new("Part") 
--change the height of p2 to help test the correct placement
p2.Size = Vector3.new(4,5,2)
--some color for clarity
p2.Color = Color3.fromRGB(0, 170, 0)


--place part one a little off center and rotated on the Z axis by 45 degrees
p1:PivotTo(CFrame.new(6,5,0) * CFrame.Angles(0,0,math.pi*.25))


--anchor the first part in place
p1.Anchored = true


--now to place the second part on top of the rotated first part:

p2:PivotTo(p1:GetPivot() * CFrame.new(0, p1.Size.Y/2 + p2.Size.Y/2, 0))

--lock p2 in place
p2.Anchored = true 

--place both parts into the workspace
p1.Parent = workspace
p2.Parent = workspace

task.wait(20)

--let p2 fall
p2.Anchored = false