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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Compound Collider Pattern in Object Hierarchy (2D)

Discussion in 'Physics' started by gdbjohnson, Aug 5, 2015.

  1. gdbjohnson

    gdbjohnson

    Joined:
    Dec 16, 2014
    Posts:
    40
    Hello, I'm a bit new to the Physics stuff. I have a gameobject built of 2 child gameobjects, with a hinge and spring joint between them. To make that work, they have RigidBodies attached to each.

    At the parent level, I have collision detection code in place that normally would work fine in a Compound Collider scenario. However, due to the rigidbodies in my child objects, the Collision messages are not being propogated to the parent.

    I'm starting to consider some "hacky" solutions as a way around it. However, I am certain this must be a common problem that has an optimal pattern for solving it, and I'm just not seeing it. With a complicated object requiring RigidBodies for joints, how does one centralize code for handling collision detection for the object overall?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    You can't explicitly control where the callbacks go, they implicitly go to both colliders involved in the contact as as well to any attached rigid-body assuming it's not on the same GameObject.

    Right now, there's no events you can hook into which would potentially be a better solution.

    If you have some custom calling pattern, you're probably better off performing those from the child and calling into the parent.
     
  3. gdbjohnson

    gdbjohnson

    Joined:
    Dec 16, 2014
    Posts:
    40
    Fair enough. I ended up working out an alternative that was similar to catching it in the children, and then calling it on the parent. But, it's much better to simply avoid it.

    When I learned about CompoundColliders (which I did not see in the manual, btw), I thought it was an interesting idea regarding inheritance. It would be interesting to have child objects' components generate events that the parent would have visibility to and decide to catch and even override.

    Anyways, thanks for getting back to me on the question.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    A compound collider is where multiple colliders attach to a single rigidbody which can be anywhere anywhere up the parent hierarchy and contact messages work just fine that way. This parent-attachment is obviously done implicitly.

    The problem comes when you add multiple rigidbody in a hierarchy and expect contact-events from that rigidbody and its colliders to somehow be sent to arbitrary other GameObjects (possibly in parents).

    Doing all this with events is something that is certainly for the future and will happen.