Align part to surface

This is a great example of what I want to achieve:
(The black ball is the part’s top normal)

The small part must be in the green box position, which means I don’t want it in the same position as the one next to it.

Need this via code and the it must work even if the parts are upside down
I have the black ball position if that helps.
Thanks for reading.

Solved!

Code snipped if anyone has the same problem in the future:

function AlignToSurface(part: BasePart, anchorPart: BasePart, AlignTo)
	local Normal = Vector3.FromNormalId(AlignTo)
	local partSurface = part.CFrame * (math.abs(part.Size:Dot(Normal) / 2) * Normal)
	local anchorSurface = anchorPart.CFrame * (math.abs(anchorPart.Size:Dot(Normal)/2) * Normal)
	local displacement = anchorSurface - partSurface
	local anchorPartWorldSpace = anchorPart.CFrame:VectorToWorldSpace(Normal)
	
	local alpha = displacement:Dot(anchorPartWorldSpace)
	part.CFrame = part.CFrame + (alpha * anchorPartWorldSpace)
end

AlignToSurface(smallPart, bigPart, Enum.NormalId.Top)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.