Search Unity

Character pushing each other (ARG!)

Discussion in 'Physics' started by LightStriker, Jun 10, 2016.

  1. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I'm about to give up, I tried everything and I spent days on this forum and google trying to find a solution to something that should be plain simple; make sure characters don't push each other.

    The problem;

    1) Have players, enemies, invaders, supporters and non-playing characters. Each with their own layers so they can attack and damage each other.
    2) All of them should not push each other. Even an enemy cannot push another enemy.
    3) They cannot cross each other. An enemy should collide with another enemy.

    Changing weight does nothing, there's always one lighter who get pushed.

    I tried adding a children with a larger kinematic collider... it works, but characters on the same layer cross each other.
    If I put that kinematic collider on a layer that everybody collide with, the character collide with is own children and is send flying. Any way to prevent this? Make a rigidbody ignore its children?
     
    Last edited: Jun 12, 2016
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Just use Physics.IgnoreCollision() (there is a Physics2D version as well). It's for ignoring collisions between two specific colliders, even if they are on interacting layers. Put it in Start() for each actor.
     
    CatsAndOwls and amirheb like this.
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    What the... How did I never saw that method after spending so much time in the Physics class?
    That sounds amazing. Testing that right away.

    EDIT: It is amazing and works perfectly! Thanks!
     
    Last edited: Jun 13, 2016
  4. omermecitoglu

    omermecitoglu

    Joined:
    Jul 24, 2017
    Posts:
    5
    wow thx! this saved my day!
     
  5. xXDarQXx

    xXDarQXx

    Joined:
    Jan 3, 2021
    Posts:
    5
    Hyblademin, you're an f-ing saint
     
  6. ArminRigo

    ArminRigo

    Joined:
    May 19, 2017
    Posts:
    20
    Excellent trick, thanks! Instead of Physics.IgnoreCollision(), you can also use more layers. Let's take the minimal example of two layers A and B. The A-A and the B-B interactions should work normally, but in the A-B interactions, we don't want B to move. To do that, add a kinematic children Rigidbody to the B objects like you suggest, but assign the subobject the layer C. Then in the player physics settings, in the layers matrix, pick the correct combination: we want to select A-A and B-B and A-C, but none of the other combinations of A, B or C.