Search Unity

Can OnCollisionEnter2D contain more than 1 collision?

Discussion in 'Physics' started by Goty-Metal, Jun 7, 2020.

  1. Goty-Metal

    Goty-Metal

    Joined:
    Apr 4, 2020
    Posts:
    168
    So i have a heavy object that falls and crushes everything on its path down, i'm using "OnCollisionEnter2D" to defect the other GameObjects but i don't know if it's possible since "collision.collider" just returns ONE collider, so the questions would be:

    1. Can OnCollisionEnter2D contain simultaneous collisions in the same call and how to get all the collision colliders?

    2. If not, which is the order to call OnCollisionEnter2D if it hits 2 colliders in the same frame/fixedframe ? random?

    Thanks!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    It should be clear it does not because the callback only refers to a collider pair which is what a collision is in any physics engine. It does however return multiple contact points.

    The order is undefined because sorting the callbacks into some arbitrary order is wasted CPU time. In the same way as you shouldn't rely the order of the Update/FixedUpdate called on GameObjects, you shouldn't for collision/trigger callbacks. Of course if you want that you are free to gather all the callbacks then sort and handle them yourself. Alternately don't rely on callbacks and just ask for contacts.
     
    Goty-Metal likes this.
  3. Goty-Metal

    Goty-Metal

    Joined:
    Apr 4, 2020
    Posts:
    168
    Great, so the final question is, is the "OnCollisionEnter2D" going to be called PER collider or if it's a simultaneos collisions it'll just pick one random and i have to get all collision points to make sure i don't miss anyone?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    I'm not sure I follow what you mean by "simultaneous collisions". A collision is defined by two colliders in contact which can have multiple contact points. There's no directionality here and these are calculated sequentially so there's no one collision happening before another. For a collision pair, the callback happens on each collider to inform each. It's also called on the attached RB of each if the RB isn't on the same GO.

    When a collider pair come into contact they can produce mutiple contact points. These are provided in each call and are the same for each collider because it's the contact points they share. If a collide is in contact with ten colliders then you'll get ten callbacks.

    As I said though, you are free to use callbacks individually and/or gather them up or just query what a collider is touching (or if it is touching anything). Lots of options but yeah, you should not rely on an order because collisions are effectively random.
     
  5. Goty-Metal

    Goty-Metal

    Joined:
    Apr 4, 2020
    Posts:
    168
    I've been testing but didn't came to a solution yet, by simultaneous collision example:
    Rock falls and hits 2 other gameobjects at the same time, exactle the same frame (or fixedupdate frame).
    So if the rock has a OnCollisionEnter2D, which of both would be as parameter for the event? would that frame call the event twice one per gameobject?

    Thanks!