Stacking boxes issue

I am making a game where I need 10 boxes x 10 boxes stacked on top of each other like a square all together.
I have made this game already in Buildbox but I am converting it. In BB it is just to stack and set the boxes to dynamic.

If I try this in Unity and set all to rigidbody they all fall down. In fact I cannot even stack 6 boxes on top of each other with out them falling as soon as I hit play.

I have tried to set enable adaptive force which was a tip found but that will not help much. Cannot use extreme drag either.

Any good solution to this crap dynamic? Would not happen in the real world:)

Change the physics step, and put collision on continues. Also use a physics material
That might help

Yes, you have to learn how to use the simulation and its settings; this, after all, isn’t the real world. :wink:

Shopping List of things to learn:

1 Like

Hi
Thanks for replying and I think I understand the different steps, but the boxes are behaving very strange. I have set the rigidbody to interpolate and continuous dynamic. I have a physics material set to 0 bouncing (also tried to change friction on both parameters).
The solver is on 6 and 1 but I changed both upwards in steps to 255 (seems to be max) but boxes still falls down.
The scale of the boxes is 1x1x1.

See video:

To note, interpolation does not affect the physics, it’s purely for visuals and changes the Transform only, not the body pose.

Right, so this shows the problem. Your description made it sound like they were not stable and were falling on top of each other, falling over etc. They are clearly being pushed sideways so that means the solver is pushing them away from each other meaning they are overlapping when you start the simulation.

You cannot place them numerically next to each other and consider this just touching; to the physics engine, they’ll be overlapping because the physics engine uses a contact offset (small gap) to ensure stability so they are considered touching defined by a distance of: https://docs.unity3d.com/ScriptReference/Physics-defaultContactOffset.html. In other words, they’d never actually come to rest as you’re starting them; they’d be slightly separated and then go to sleep.

This doesn’t mean you should try setting this to zero to “solve” the problem. It means they should initially be at least this far apart from each other.

Try a single stack where each items is at least this far apart. Get that stable using the settings described previously then you can add more stacks next to each other, again, at least this distance apart.

Please note though, I’m not a 3D physics dev but a 2D physics dev so this is a mostly guestimate on what is is going on here but the sideways movement indicates starting in an initial overlap state for sure.

Hm. So I used the grid tool set to 1 unit for all axis and copied them so that they are basically 0 to each other in all directions. Are you saying I need to move them in all directions in 0.01 or something to make them not interfere with each other on startup? This seems weird…

Well, moving them to the side a notch didn’t do anything.
Video;

Yep, I never side only doing that would. I said all the boxes are likely overlapping. This obviously includes vertically.

It’s just that the sudden “explosive” separation isn’t typically a stability issue but instead an instantanteous impulse where the solver is trying to separate things.

They need to be at least separated by: https://docs.unity3d.com/ScriptReference/Physics-defaultContactOffset.html

I just did a test with a prefab (box collider, rigidbody set as continuous dynamic) and it works if they are separated.

I tried this as a rough test script:

using UnityEngine;

public class Separate : MonoBehaviour
{
    public GameObject Cube;
    public int ColumnCount = 10;
    public int ColumnStackCount = 8;

    // Start is called before the first frame update
    void Start()
    {
        var stride = 1f + Physics.defaultContactOffset;

        for(var x = 0; x < ColumnCount; x++)
        {
            var basePos = Vector3.right * x * stride;

            for(var y = 0; y < ColumnStackCount; y++)
            {
                Instantiate(Cube, basePos + Vector3.up * y * stride, Quaternion.identity);
            }
        }
    }
}

That said, even without the extra offset I don’t get the explosive effect you get, just a little more rocking until it settles i.e. if I do:

        var stride = 1f;// + Physics.defaultContactOffset;

Note that if I increase the solver iterations, they don’t sway at all and are rock solid.

I just tried this with a 32 column, each with 32 blocks and ramped the iterations to max just to be sure and it was stable.

Hmm. Cool. I don’t do manual scripting. I work with Visual Scripting. Could you send that script so I can test?

It’s above, just paste it into a script file named the same as the class.

Here’s the project.

8469632–1125452–Boxes3D.zip (112 KB)

You’re talking over my head. I suck at this. I am getting compile errors…

Assets/Material/Separate.cs(3,14): error CS0101: The namespace ‘’ already contains a definition for ‘Separate’
Assets/Material/Separate.cs(10,10): error CS0111: Type ‘Separate’ already defines a member called ‘Start’ with the same parameter types
ArgumentException: The Object you want to instantiate is null.

Wait, I will try the zip.

Okay, so your project worked and I cannot see what is different from mine. What does the the script actually do? Offset cubes just a tiny bit sides and up and down?
I would use this script but the problem is that I need all cubes to have different individual scripts, colors etc:)
Could you try to setup the same without a script and see what happens?

Yes, as we’ve been discussing.

It’s not supposed to be for anything other than showing you that it works.

I have no idea what that means. If I create these manually they’ll do exactly the same thing. It does matter how you create them.

There’s nothing wrong with being inexperienced but let’s not think that it’s how you created them that causes the problem so me doing more work for you won’t reveal anything. The script is super simple. You’re not going to be able to create games without understand basic C# and writing scripts. Visual Scripting will only get you so far. There’s plenty of tutorials out there that will help you with this. I would start by looking at the Unity Learn site.

I think I can get quite far in my game development with visual scripting as I have already made some cool games:)
Of course I’d like to learn the C# but I love to do 3D, make the concepts etc. Programming is so far too hard to get my head around. Also I think knowing that one needs to adjust things in the project settings for gravity stuff to work, is as little to do with C# as Visual Scripting:) Haha.

But anyway I redid everything and now it is somehow working…I have no idea what the reason why…Just happy that it worked!
Thanks for all the help:) It got me where I wanted!

1 Like

That’s perfectly fine. Having fun creating stuff is a huge part of it. Good luck and enjoy!