Help with stacking models on top of each other

Hey, I’m working on a luggage system to make suitcases/luggages stack on the cart and I’m struggling to stack them on top of each other.

First if there is no luggage on the cart I want to stack the luggage (model) on top of that part and if there is already a luggage I want the new luggage to stack on top.

I end up with a gap between the part and the first luggage. I also end up having the 2nd luggage overlap with the first one.

Basically I need help with placing a model on a part and placing a model on top of a model.

image

Here is my code:

	if #carrying == 1 then
		print('first')

		local surface = cart:FindFirstChild("Surface")

		if not luggage.PrimaryPart then
			return
		end
		if not surface then
			return
		end

		luggage:MoveTo(surface.Position + Vector3.new(0, surface.Size.Y / 4 + luggage.PrimaryPart.Size.Y / 4, 0))

		local weldConstraint = Instance.new("WeldConstraint")
		weldConstraint.Part0 = luggage.PrimaryPart
		weldConstraint.Part1 = surface
		weldConstraint.Parent = luggage.PrimaryPart

	elseif #carrying > 1 then
		local lastLuggage = cart:FindFirstChild(carrying[#carrying - 1])
		
		if not lastLuggage or not lastLuggage.PrimaryPart or not luggage.PrimaryPart then
			return
		end

		local lastBottom = lastLuggage.PrimaryPart.Position - Vector3.new(0, lastLuggage.PrimaryPart.Size.Y / 2, 0)
		local newHeight = luggage.PrimaryPart.Size.Y

		local buffer = 0.05

		-- Calculate the new position with an additional small buffer to avoid overlap
		local newPosition = lastBottom + Vector3.new(0, newHeight / 2 + buffer, 0)

		luggage:PivotTo(CFrame.new(newPosition) * CFrame.Angles(math.rad(0), math.rad(-90), math.rad(-90)))

		local weldConstraint = Instance.new("WeldConstraint")
		weldConstraint.Part0 = luggage.PrimaryPart
		weldConstraint.Part1 = lastLuggage.PrimaryPart
		weldConstraint.Parent = luggage.PrimaryPart
	end

please send a clip of the bug bugging please :smiley:

Do the position of the last luggage (it’s center) + half of it’s height + half of the new luggage height is the new luggage position

local newPosition = lastBottom + Vector3.new(0, newHeight / 2 + buffer, 0) --this should be lastTop instead of lastBottom

you should be getting ‘lastTop’ and adding half of the newheight to it, instead of lastbottom. why the new luggage is being placed clipping ontop of the last luggage instead of directly in the middle of it is absolutely beyond me (which is why it took me 30 minutes to think of any answer)

I’m still having overlap between the suitcases, the size of the primary part btw is the same size of the model’s GetExtentsSize()

local lastBottom = lastLuggage.PrimaryPart.Position - Vector3.new(0, lastLuggage.PrimaryPart.Size.Y / 2, 0)
		local newHeight = luggage.PrimaryPart.Size.Y

		local buffer = 0.05

		local newPosition = lastBottom + Vector3.new(0, newHeight / 2 + buffer, 0)
		--lastBottom + Vector3.new(0, newHeight / 2 + buffer, 0)

		luggage:PivotTo(CFrame.new(newPosition) * CFrame.Angles(math.rad(0), math.rad(-90), math.rad(-90)))

When I remove the / 2 in newHeight / 2 it goes up, so I think we gotta find a way to calculate how much should be divided in newHeight / 2

why do you need the cframe.angles

To make the luggage lay on its back

im not sure if i have this completely accurate, but from what i can tell you are using the wrong axis + a wrong formula which is what is causing it to position incorrectly

since the calculations use the y axis for all of their math, when you rotate it onto its back, everything gets thrown off (since the height is completely wrong)

you change the orientation, but the SIZES never change. switch your axis and itll probably work

Okay so I with setting the orientation with CFrame.Angles and switching the axis to X and then -X there is still overlap…

		local lastBottom = lastLuggage.PrimaryPart.Position - Vector3.new(0, lastLuggage.PrimaryPart.Size.X / 2, 0)
		local newHeight = luggage.PrimaryPart.Size.X

		local buffer = 0.05

		local newPosition = lastBottom + Vector3.new(0, newHeight / 2 + buffer, 0)
		--lastBottom + Vector3.new(0, newHeight / 2 + buffer, 0)

		luggage:PivotTo(CFrame.new(newPosition) * CFrame.Angles(math.rad(0), math.rad(-90), math.rad(-90)))

image

try

local lastTop = lastLuggage.PrimaryPart.Position + Vector3.new(0, lastLuggage.PrimaryPart.Size.X / 2, 0)

local newHeight = luggage.PrimaryPart.Size.X

		local buffer = 0.05

		local newPosition = lastTop + Vector3.new(0, newHeight / 2 + buffer, 0)

		luggage:PivotTo(CFrame.new(newPosition) * CFrame.Angles(math.rad(0), math.rad(-90), math.rad(-90)))
1 Like

Alr, so that did work with those suitcases but when I try suitcases with a different size I get a gap between them…

Okay wait nvm, after doing some tweaking with the X and Y I’ve got it working! Tysm for the help man!

1 Like

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