Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

compositeCollider2D OnCollisionEnter2D receive child gameObject

Discussion in '2D' started by xtoc, Sep 30, 2017.

  1. xtoc

    xtoc

    Joined:
    Jun 3, 2017
    Posts:
    15
    Hi,

    I'm using compositeCollider2D as parent object. This parent object includes multiple child objects (with boxcolliders2D).

    When OnCollisionEnter2D is been hit, it always refer to the parent gameobject with the compositeCollider2D attached.

    Is there a way to receive which child is been hit?

    Thank you in advance!

    Kind regards,
    Kurt
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    No. because there is no longer any collision geometry in the physics system specifically identified with the colliders being composited. Indeed, compositing isn't a lossless process because geometry specific to a collider can be lost or shared between multiple colliders.

    So in summary, it's not hitting a child collider, it's hitting the composite which is the only active collider in the physics system. You can see this by rolling out the "Info" on the child colliders and note that there are no shapes generated, only on the composite.
     
  3. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    @MelvMay Still:
    Knowing 3 colliders composing a composite collider;
    Collider2D _colliderA, _ColliderB, _ColliderC;

    Is it still possible to check for "ColliderB.OverlapPoint(Vector2 somepoint);"? (I've got to admit I don't know a lot about how colliders work). Based on what needs to be checked and how, it could be a nice workaround. If this doesn't work, what would happen instead?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    As I just said, that information isn't created so it cannot be queried either. Queries like OverlapPoint, Raycast etc perform an intersection test against the low-level physics objects i.e. circles, polygons, edges etc. The colliders used by a composite don't create any low-level physics objects at all. Only the composite does.

    The composite isn't mean to be a representation of each of those colliders at runtime but a single collider that takes over for multiple colliders merging this geometry into one. Any individual collider details has been lost and isn't able to be referenced or addressed via the geometry created by the composite.
     
    Last edited: Sep 30, 2017
  5. xtoc

    xtoc

    Joined:
    Jun 3, 2017
    Posts:
    15
    Thanks for your answers!
    I think i've solved by collecting every related gameobject and add it in List<GameObjects> _allBricks;

    This is how i decide which block is been hit :
    _allBricks.OrderBy(x => Vector2.Distance(_collider.contacts[0].point, new Vector2(x.transform.position.x, x.transform.position.y))).FirstOrDefault();
     
  6. yuvalko

    yuvalko

    Joined:
    Dec 4, 2018
    Posts:
    2
    xtoc,
    this can be done more efficiently:
    you don't have to order - just to find the lowest distance.
    Taking your example - it will look like this:
    _allBricks.Min(x => Vector2.Distance(_collider.contacts[0].point, new Vector2(x.transform.position.x, x.transform.position.y)));