Hi there
I’m trying to make a small "stacking game, where the goal is to stack some kind of identical boxes to make the taller pile possible.
Everything works fine, except for one issue I can’t figure out how to solve :
I’ve noticed that when the stack contains 4 or 5 boxes, each time I add another one on top of the stack, some of them keep sliding a bit and a bit, until all the stack become to unstable and fall. This also happens when the boxes are strictly aligned withe each other.
What I’ve tried so far :
Tweaking the Velocity Iterations
Adding a maximum friction material to the boxes
Increasing the mass of the boxes
Creating a FrictionJoint2D when a box collide with the box beneath it. The joint keeps breaking a frame later being created
FrictionJoint2D joint = gameObject.AddComponent();
joint.anchor = new Vector2(0.64f, -0.67f); //bottom of the collision collider
joint.connectedBody = target.gameObject.GetComponent();
joint.enableCollision = true;
joint.maxForce = 100000;
joint.breakTorque = 0;
You will find a screenshot of the spawned prefab in attachment.
From what I understand, when reading other similar posts, this is a quite common behaviour when working with stacked objects.
If any of you have an idea on how I can fix this issue ? I guess I'll need to find a workaround to this behaviour, but don't really know where to start :smile:

The physics engine is really stable with stacks but stacks, as they get higher, get more unstable so need to increase the standard number of iterations in the project settings. It’s there so you can increase it for more stability for stacks, joints etc. You’re not talking about large stacks though.
If you’re having problems then you’re likely doing something to update the simulation but you don’t need to use friction joints.
Maybe modifying Transforms, the Colliders or Bodies. Also, if you making the colliders bouncy or low friction it’ll make it more unstable. Also, depending on how you stack them it can cause problems, for instance if you instantly create something on the stack which is overlapped or falls from a height onto it.
On top of this, as long as you’re careful you can also use continuous collision detection to make it more stable but it’s more expensive.
Also, keep your mass ratios similar or the same. A huge mass on a smaller one is tricky and gets hard to keep the forces in check.
Take a look at my PhysicsExamples2D repo. There’s a whole bunch of things in there include showing stacking of boxes, circles, capsules which are all super stable and then come to rest and sleep.
Here’s a bunch of capsules that fall onto each other and stack and then sleep. You can then drag them to make them topple over:
I’ll try to have a look at Transforms, Colliders and Bodies then. At the moment, Colliders are set to no bouncy, and max friction.
Increasing the iterations helps until a certain point where I don’t see any noticeable improvement.
I’ll try to reduce the height from where the boxes are falling onto the stack.
Reading your reply made me think of a detail I forgot to mention, don’t know if this is relevant but : my game adopt a kind of isometric view. So the collider is not a simple square or rectangle, but parallelogram. This is the reason I had to set the friction to maximum, if not each object would slide. You
Well yes, that’s quite different. Because they are on an incline, you’ve got a lot of sideways forces wanting to push them apart. I’m not sure that should be a physics thing really i.e. friction/gravity keeping them balanced. Might be better to just place them then constrain the Rigidbody2D or set its body-type to Static when it’s on there?
And added in script the following behaviour : as soon as the falling object collides with the other ones in the stack, mass is set to a much higher number.
This way I got rid of the unrealistic slippery effect !
Was just fiddling with this recently… the more objects you have stacked atop each other, the more sensitive the physics system becomes. This is because the solver has to figure out exactly how all those pieces in contact resolve to a cozy custom state.
To see what I mean, make a for() loop to stack Rigidbody cubes right on top of each other and just wait.
As you stack more and more and more, they become less and less stable. Your solution of changing mass works, but it may not work when you have 100 objects in a stack, so you might want to test further.
Here was my tinkering testbed: just drop it on a blank GameObject and see what I mean:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker
//
// beyond a certain number of stacked contacting rigidbodies,
// the solver can't get them quiescent enough to sleep them.
//
// make an empty scene and drop this on a GameObject.
public class PhysicsStack : MonoBehaviour
{
[Header( "Adjust to see if it can sleep.")]
public int count;
void Reset()
{
count = 15;
}
IEnumerator Start ()
{
Physics.gravity = Vector3.down * 9.8f;
var pos = transform.position;
for (int i = 0; i < count; i++)
{
var cube = GameObject.CreatePrimitive( PrimitiveType.Cube);
cube.transform.position = pos;
// bottom cube is larger and has no Rigidbody, it is the fixed base
if (i == 0)
{
cube.transform.localScale = new Vector3( 2, 1, 1);
}
else
{
// enable this...
var rb =
cube.AddComponent<Rigidbody>();
// ... and try tinkering with sleep threshold
rb.sleepThreshold = 0.0f;
}
pos += Vector3.up * 1.05f;
yield return null;
}
}
}