stop box colliders pushing each other

Hi, so I have an enemy with a box collider set to chase my player which also has a box collider and a rigid body 2d. However, when my enemy gets close to my player, it pushes the player through walls in my tilemap. Does anyone know how to fix this? any help is much appreciated thanks.

They cannot stop each other overlapping without “pushing” each other. You likely want them to not overlap but to not “push”. They are one and the same thing.

You need to define what “walls in my tilemap” are. If these are polygons then that’s a big gap to “push” through which likely means you’re using discrete collision detection and are moving these things very fast or that you’re constantly causing overlaps because you’re modifying the position directly, probably through the Transform which isn’t something you should be doing.

If none of this applies to what you’re doing then you need to provide some real information on what we’re talking about here because the description above lacks any detail to help you in specific terms.

2 Likes

I just noticed this. It sounds like your “enemy” only has a BoxCollider2D and not Rigidbody2D. This means it’s implicitly Static (non-moving) so cannot be moved. This means you’re modifying the Transform to do this because there is no Rigidbody2D to use (how you’re supposed to move stuff).

If you place such an “enemy” overlapping the Player, you’ve got a Dynamic Rigidbody2D (player) who is suddenly overlapped by a Static Collider (which cannot move) so the physics engine has to solve this by moving the Dynamic Rigidbody2D out of overlap.

The enemy here act very much like the Static level such as your tilemap.

1 Like

Ah yes this was it, thank you very much, I’m still rather new to unity so sorry if this was a silly mistake. My enemy now doesn’t push my player through walls but is there any way I can stop both rigid body’s from being able to push each other as my enemy can still move my player (and vice versa).

As I said initially, all the physics system is doing is stopping them overlapping. It’s not “pushing”. It cannot stop both of them overlapping without doing that.

In Unity 2022.2 (beta) you can control force send/receive but this can only apply to one side of this contact. If both sides don’t do it then they just pass through each other. If you’re new then I wouldn’t expect you to use this new feature though.

You can, of course, do your own Kinematic motion by using queries to determine where you can move to without overlap then moving there. That isn’t something I’m going to write how to achieve here though.

1 Like

okay thank you, I’ll have a look further into these.

1 Like