- Home /
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.
Thanks!
If you have rigidbodies (not is$$anonymous$$inestmatic) and actual Colliders (not isTrigger) than no, you'll get collisions. You can look at both of those options and physics interactions to tweak what you want to happen, or you can clarify what it is you DO want to happen.
I think I'll have to do that. I'll have to code my own physics I guess.
Answer by ypsilon · Apr 08, 2014 at 04:16 PM
This is not an actual solution to this problem, but it is how I've ended up doing it:
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)
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.
Each character has one or more trigger colliders to detect colliders coming from the next point:
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.
This is a great sollution, was trying to find a way achieve just this. For clarification for anyone finding this answer, to disable collision between layers you make changes in the Layer Collision $$anonymous$$atrix (the thing that looks like a triangle of checkboxes) in the appropriate Physics $$anonymous$$anager.
For 3D collisions check here https://docs.unity3d.com/$$anonymous$$anual/class-Physics$$anonymous$$anager.html
For 2D collisions check here: https://docs.unity3d.com/$$anonymous$$anual/class-Physics2D$$anonymous$$anager.html
Answer by MileSplit · Apr 02, 2014 at 03:08 AM
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.
Effectively, if I increase the enemy's mass I'm not able to push it with the player. But the enemy would be able to push my player then... So I'm at the starting point
Answer by arkariarn · Apr 02, 2014 at 10:57 PM
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.
It's a good point, although it doesn't seem consistent to me to have the physics of the player be handled by unity and the ones from the enemies to be handled manually.
Answer by Cornelis-de-Jager · Mar 23, 2018 at 04:06 AM
All the objects in the game you can make Kenimatic . As for the players, lock their X and Z position.
Your answer
Follow this Question
Related Questions
Detecting when Player stoped to push a rigidbody? 3 Answers
Making Colliders/Triggers or Rigidbodies move a Character Controller 5 Answers
How to make certain rigidbody2d's not able to push other rigidbody2d's? 2 Answers
How to prevent a rigidbody from pushing other rigidbodies? 1 Answer
C#: "Pushable" obstacles for a puzzle 0 Answers