- Home /
How to avoid Rigidbody collider from getting stuck between walls
Hello! I have a 2D physics problem where a RigidBody2D BoxCollider2D which is set to never sleep and continuous collision detection gets stuck between two static colliders. Setting the rigidbody collision detection mode to discrete solves the problem but we have mechanics which involve stacking boxes on top of each other and using discrete collision detection the stack of boxes will most often collapse.
I have tried using rounded corners, use frictionless material, use edge colliders for everything, use crazy gravity values like 1000 but the box still gets stuck between the walls.
The scale of the colliders is unit size and their physics material is frictionless. Also the gap between the two highlighted black walls is 1 unit so the collider should pass through the gap.
This is the only remaining major issue which is giving us a lot of trouble while doing level design. Right now our only solution is to increase the gap by slightly offsetting the colliders but this is not a viable solution in all situations. Any kind of help will be really appreciated.
Thank you.
Answer by YesNoKonrad · Oct 18, 2016 at 01:34 AM
Well this is only logical.
Think of it: If your boxes, tiles and gaps are exactly 1 unit wide and a box and a gap are exactly aligned, the outer x-bounds of the collider of the box exist at the same position as the tile-bounds surrounding the gap. No matter the friction: since the collider-lines align, a collision is registered.
No choice (except there is) than to reduce the box's collider size by a miniscule amount. I recommend at least 4% collider-size- reduction. You will see this in every game in existence that the boxes are smaller than the tiles the level is built off.
You should also probably adjust the sprite, however leave the transform at normed scale. Just to be sure. Unity loves to do weird stuff on its own. Let's don't give it any more reason to do so.
Another solution, by the way, is to make the box-collider into trigger and write your own gravity handling function. Whenever the trigger function is called you observe the relative movement of the two involved objects and make a decision based on that.
I never thought of the second solution even though I thought about writing grid based collision but this is even better. Thanks man!