Search Unity

Collision with Child Object Calls Parent Objects OnCollisionEnter2D

Discussion in 'Physics' started by James-Sullivan, Dec 24, 2015.

  1. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    I have two gameObjects each with colliders. ObjectA is parent to ObjectB. I noticed that whenever something collides with the child object, ObjectB, only ObjectA's OnCollisionEnter2D methods are called. Why does this happen?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Callbacks are performed on both the Collider and the Rigidbody component GameObjects. If the Collider is on the same GameObject as the Rigidbody then it'll only be called once for that GameObject.

    In other words, both the Collider and Rigidbody are notified of the collision or trigger.
     
    ThinhHB likes this.
  3. James-Sullivan

    James-Sullivan

    Joined:
    Jun 15, 2015
    Posts:
    128
    I have a parent object with a collider2d, and a child of that parent also with a collider2d. When something collides with either object, how can I tell with which collider (parent or child) the collision occurred with? Looking at the collision information provided in a Collision2D, I don't see how this can be accomplished since OnCollisoinEnter2D is called on scripts on both parent and child objects.

    Edit: The solution is to give the child object a kinematic Rigidbody2D.
     
    Last edited: Dec 30, 2015
  4. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    Seems like we need to add Rigidbody2D's to the children if we want OnCollisionEnter2D to be called.