Localising RigidBody physics to X and Y plane

Hi all, really hoping for some techy help here, it’s a hard one to explain so I have pictures!

I’m trying to drop RigidBodied spheres onto a container and have them locked on Z so that they don’t move forward, just sideways and vertically. But then I want the container holding them to rotate and for the spheres to maintain this sideways and vertical-only movement but at the new angle. All my attempts have the spheres falling from the container as they are adhering to the original frozen Z plane.
I’ll attempt to illustrate this problem for clarity:

The red spheres drop into the yellow container (more specifically they drop onto the blue targets, screen 2) and are children of the container. As the spheres continue to drop I want them to then stack up but keep in line at the same Z coord, so I freeze the Rigidbodies on Z. This holds them in a neat and tidy stacked pile.

But I want the balls to jiggle and move around sideways (green arrows) and drop into gaps as other spheres around them are removed. But still remain at the same Z position, so that they remain stacked.

I have this working in my scene no problem with rigid body sphere/Gravity, various box colliders and a sphere-destroy function.
However I then wish to rotate the yellow container (the parent of all red spheres) by 90 degrees on Y plane like so…

and still maintain their Z position with the yellow container as they drop and fill vacated gaps, moving sideways like the green arrows and thus remaining stacked. Because of the new angle the (frozen) Z plane of the sphere needs to be modified.

How can I change the container angle and have the spheres (falling as needed onto the blue targets) adhere to the same Z plane as the container at its new angle, and continue stacking upwards and sideways only?

In the rigidbody settings, find Freeze Position, and check the box next to Z. If you did this, you’d have to rotate the world around the container though.

Just a thought … why not consider adding two transparent colliders in front and in the back. These could simply be two empty game object to which you attach box colliders and scale them to block the front and back.

This would spare you from having to mess with limiting the local Z movement. These two game objects would be parented to the yellow object which would also allow you rotate and move the thing anywhere you want.

Had a few minutes to spare so I went ahead and build a prototype to demonstrate what I was suggesting.

Here is a short video demonstrating the suggestion.

Hey Stephan,

Thanks for that answer, and the effort you put into that video solution. I had originally tinkered with such a front/back solution but didn’t get good results. However your mention of using a non-friction surface has pointed me to where I got the setup wrong. I will try again with such a setup.

Thanks again for your effort, much appreciated.

Welcome and good luck :slight_smile:

You can probably set the whole thing as a child of a gameobject, and each frame force localPosition.z = 0; - it shouldn’t affect physics any (in this situation), but will force them to keep their local z.

You’d then just rotate the single parent gameobject.