Character getting stuck inside tilemap colliders

Sometimes an enemy can push my player into the wall and the player will be stuck.

I am using Unity Tilemap and if I use the Composite Collider, it seems easier to get pushed in, and once inside, the player can move around freely within the wall, but is blocked from escaping out of the wall.

With Composite Collider turned off, it seems hard to get pushed into the wall, but when it happens, the player is also stuck and can’t move much, just sort of wiggle a bit but I haven’t been able to escape.

What can I do to prevent this from happening or what can I do to recover from it if it does happen?

The physics engine won’t ever cause overlaps like this so you must be doing it. In nearly all cases it’s because you’re modifying the Transform bypassing physics by teleporting stuff around instantly or in less cases, you’re creating an impossible situation for the solver to solve i.e. there’s no where to move and there’s overlaps.

Also, Colliders don’t solve overlaps. The physics engine doesn’t know about Unity Colliders at all, not even a little. A CompositeCollider2D isn’t known it it.

It only knows Circles, Capsules, Polygons and Edges. A CompositeCollider2D can produce either Edges (Outline Mode) or Polygons (Polygon Mode).

Edges are not closed shapes so don’t have an interior. You’re less likely to overlap badly a Polygon than an edge because when you go past the edge you’re on the other side, not inside. I bet your composite is set to outline. Set it to polygon and it’ll be the same as if you were not using it.

The problem though is why/how you are creating such overlaps and nothing caused by the above.

My setup is like this: I have a Player with a Capsule Collider 2D and a Rigidbody set to Dynamic, Continuous Detection, and Interpolate set to Interpolate. I move the player with rigidbody physics by the velocity.

The player gets pushed in the wall by enemies. My enemies also have a Capsule Collider 2D but no rigidbody and are moved by different ways depending on the enemy. Some by transform.Translate, some by setting the transform.position to Vector2.MoveTowards (either MoveTowards the player or MoveTowards the next section on its path).

So the Enemy are Static colliders which shouldn’t be moved. You can tell that because to “move” them you’re instantly modifying the Transform because you don’t have a Rigidbody2D. You should never be doing this.

Regardless, the Player is a Dynamic body and has a collision response so you’re putting an Static Enemy overlapping it and on the other side is a Static tilemap. The only thing that can move in this situation is the Player and it’s an impossible situation to solve.

I mean, ask yourself. What should it do? Stop the Enemy from pushing it? It can’t, it’s a Static Collider. It’s like asking it to move the tilemap which is also Static.

Oh, I didn’t know. I got that technique from different tutorial videos I watched. What is the correct way to do this?

That’s too broad a question and would take too much to type out how to write enemy character controllers etc. Before you knew it, I’d be writing a tutorial.

Honestly, there are plenty of tutorials out there for movement of 2D characters, enemies etc. Seems you found a bad one showing moving an immovable Static object (Enemy) into things that can move (Player).

Add a rigidbody2d and use moveposition?