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.
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
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)
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