How to keep a GameObject inside a collider?

Say I want to make a sphere stay inside a box’s boundaries. I tried to create a box collider for a GameObject and put a sphere inside, but that didn’t work. The sphere fell through. Is it possible to create a boundary for a character and how?

You can try to create colliders around the area you want to move your sphere within!
You can do this without any code, you just need to setup static colliders to collide with objects on certain layer and put the sphere you wanna move on that layer.
This will help: Unity - Manual: Layer-based collision detection

Things are only able to collide with the “out side” of a collider (which you can’t look through, whereas you can look through the inner side). Try to make it with several colliders or do it with script.

You could create the boundaries with 6 box colliders like in the image, that way a sphere with a rigidbody will stay inside and react depending on the parameters you set.


Is there any way to do it then?

If you are working on a 2D project you could try to use an Edge Collider 2D, it is working for me. You basically draw the edge of any shapes, just bear in mind that is not super precise.