Player and enemies occasionally clip through walls.

Hello all!

I have an issue where my player and enemies occasionally clip through walls. I observe this most commonly when several enemies are together, bumping into each other and pushing each other along when they’re doing their attacks.

My stage/map is surrounded with an edge collider which serves as the barrier/wall/outermost limits of the map. This is the thing that players/enemies are clipping through. The enemies and players have box colliders.

Some notes/things I am doing:

  • I am using continuous collision detection on both enemies as well as the player.
  • Both enemies and players use rb.moveposition in order to move around and attack (the enemy attack is just rb.moveposition with movespeed + 50%, nothing special.
  • This clipping is not a consistent thing. It happens quite rarely, and once again, mostly when enemies clump together and are all bumping/pushing each other around.

Are there measures that I can take to ensure that this never occurs? Not gonna lie, I was thinking of just putting a second and/or third edge collider around the first one but it feels like there must be a better way.

Thank you for your time!

In the end, if these are Dynamic bodies and they are packed densely together where there’s an immovable barrier, the solver can have a hard time figuring out the “best” solution to all the overlaps. You want it to just know but the solution isn’t always possible.

Are you using the default solver iterations? You might want to give it more room to solve it if the problem is complex.

Also, using MovePosition on Dynamic bodies isn’t optimal, especially in crowded set-ups. This is because all MovePosition does is set a very high velocity so it moves to the specified position during the simulation step time. This means you might have a lot of bodies all contacting each other at high velocities so the impulses are all high too keeping them separated.

You should consider moving such things using forces. In the end, the solver isn’t aware so you have to try to not fight it and give it hard to solve problems, especially if you’re not increasing its allowed iterations.

I’ll add separately that edges are the only open physics shapes. As soon as another physics shape goes over the center portion of the line, it’ll be moved to the other side.

Using something like a large box would result in you being able to increase the region to the center point so it would always result in the solver solving the overlap to “push” it back into the gameplay area.

You could also continue to use edges but set an edge-radius so the line has a width. Make it larger than the largest thing that tunnels through it.

1 Like

I’ve set the edge radius higher (I didn’t know about that function) and it seems to have solved most of the issues. Thank you very much!