Rigid bodies not to push each other?

I’m starting a beat’em up game. I’ve implemented the player with a rigidBody and several colliders, and was thinking about doing the same for the enemies. The reason for this is that I want Unity to handle the physics for me. The way I’m moving my character is modifying the transform’s velocity.

Now my problem: if I add for example a cube (representing an enemy), with its corresponding rigid body and collider, and I make my character go walk against it, the cube will then be pushed by the player. Now, I don’t want this to happen, since I don’t want characters in general to be able to push each other.

My question: is there any way of avoiding this? I might have to change my whole approach to the implementation in case this couldn’t be done.


This is not an actual solution to this problem, but it is how I’ve ended up doing it:

  1. I have just disabled the collisions between the “Player” and “Enemy” layers, letting the different characters in the game pass through each other (if you check out other classic beat’em up titles like Turtles in Time for the SNES, you’ll find out that this is how they did it)
  2. Each character now has a rigid body and a normal collider. This collider is to check for collisions with other non-character elements, like the ground.
  3. Each character has one or more trigger colliders to detect colliders coming from the next point:
  4. Each fighting movement has a collider (for example attached to the fist in the case of punching), that will be detected by the trigger collider attached to the character being attacked.

This is the best approach that I’ve found for my case.

I had this same problem, play around with the weight values of the rigidbody for enemies and players. For example increasing the weight of an enemy should not allow it to be pushed.

I would set the collider of the enemy to a trigger and handle his movement by script. But this way he will go through everything, even walls, so maybe a simple raycast2d to check if there is a wall in a direction (you can do this by using a layermask)can do the trick:

 if(Physics2D.Raycast(transform.position, vector2.right + transform.position, 1, 1 << 8)){
//stop moving

so this would check if there is a wall to the right of the enemy at a distance of 1, given that you have made a new layer wall on the 8th layer.

All the objects in the game you can make Kenimatic . As for the players, lock their X and Z position.