Boxes are not moving together

Godot Version



Hey I made this little game where you are able to move boxes. As you can see in the video I have almost everything working but I can’t figure out what code to add or what to change so the boxes move together when you’re moving only the bottom one. I want them to be pushed together even if they are on top of each other.
Any help is appreciated!

If you are using RigidBody2D for them change friction value to 1 for both.

1 Like

Thanks for the reply but this didn’t work.

When your boxes make contact, determine their order. Reparent the top box to be a child of the lower box.

When you push a lower box, all boxes above it will automatically move with it since children all move with their parents. When the boxes lose contact, break the parent/child relationship.

1 Like

it is not bad, but he still has to check if the upper box has no collision from the side before the lower one… this is also important

:bulb: I would suggest to use Raycast2D and check what is under the box. and also if it is already in Raycast, also check for collision with the player if there are two boxes on top of each other and the player wants to suppress the top box

something like this, it’s not quite complete but as an idea

the easiest way is to update the x position of the lower box to the upper box and of course the x offset between boxes

1 Like

Thanks for the reply!

It took me alot of time to actually implement what you said because I am a beginner and when I first saw your solution I kind of felt like the face in the left bottom corner in your picture.

Unfortunately I have 3 new issues now.

Issue numero uno:
This is an issue I only realised has been there just now. The cube stays floating after picking up the one below until I touch it. I did implement another Raycast that checks if the cube is not on the floor and then applies gravity but it still didn’t change anything.

Issue numero dos:
This propably has an easy fix but I don’t know how to make it so the children also change position if colliding with a wall. Or maybe if there is an solution so that the parent cube stops if the children collide with the wall that could also work.

Issue numero tres:
This I am completely clueless about. Why does the cube get randomly stuck when I push it on top of another box? Is there even a fix to this? It also seems to happen completely random and without any reason.

If you or anyone else know a solution to just one of these it would be a big help.

it does look to me that you will just need to add child of the box that hit the top of other box
then it should move both box if you push the bottom one

also i havent tested but, if it’s possible, you can set that there’s a rigidbody physics friction that hold anything like a glue for each boxes