Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Prevent colliders from pushing each other?

Discussion in 'Physics' started by PlasticEagle, Feb 2, 2024.

  1. PlasticEagle

    PlasticEagle

    Joined:
    Dec 2, 2020
    Posts:
    1
    Hey everyone! I've been working on an enemy for my third-person combat-based game.

    I've created an enemy that moves towards the player when detected, and when it reaches the player, performs a simple attack for now.

    However, I've noticed that when the enemy is moving and the player is in the way, the enemy pushes the player, applying force to the player.

    In the above case, only the player has a ‘rigid body’, but the enemy does not. What I would like to happen is the player does not get pushed when the enemy's collider interacts with the player's capsule collider.

    For instance, in this video from God of War, we see that neither the enemy nor the player can move the other around when in contact (unless it is specifically an interaction with the other such as dealing a blow)

    Also, when the player runs into the enemy, the enemy does not get pushed, this is how I'd like my enemy to behave with the player as well.

    What's worked so far: based on what I've looked up, I used a ‘rigid body’ on the enemy with mass as 5 (the player has it as 50) and it has since made the push less apparent, however, I'm not very satisfied with this result and now the player can push the enemy around too.

    So, I was wondering if there's a standard approach to this that developers generally use that I'm not aware of, where on collision, there is no physics interaction other than a physical barrier to the each other’s ‘game object’ instead of one pushing the other.

    Is there anything else here? Or is my approach to this problematic in the first place? I would greatly appreciate any input on this, thanks!!
     
  2. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    861
    sounds a bit that you don't want to have physics at all if you want to get rid of all physics behavior. maybe you just want to stop movement of entities if they collide? for this a trigger collider would probably the easiest to use.
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,410
    If you move all things with physics, then they will behave appropriately for physical objects.

    If you move ANY thing without physics (also known as moving kinematically), then you will probably cause issues for anything else that was trying to behave appropriately for physical objects. Kinematic motion basically clobbers the physics system and the physical object will always lose.

    If you want a kinematic object to move without disrupting or passing through another object, then it is entirely your code's responsibility to probe the space ahead of the moving object and see if that space is clear. If you're moving a ball to the north by one meter, then you first check with a Physics.SphereCast() call to make sure there is nothing in the way of moving to the north by one meter. What if there is something there? It's entirely your code's responsibilty to decide how to handle that. Move 0.65 meters instead because that's where an immovable wall object is? Move northeast instead of north because the wall is at an angle that makes you slide along it? Move 0.32 meters instead because that's where a physics-based object is, and also AddForce a weak nudge to that physics-based object to simulate a gentle pushing behavior? It's aaaaaallllll up to you.