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 Colliders and Trigger Scripts

Discussion in 'Scripting' started by swtoasty, Jul 8, 2009.

  1. swtoasty

    swtoasty

    Joined:
    Dec 10, 2008
    Posts:
    25
    After lots of frustration and forum searching I've made some progress with compound colliders, however, one problem still frustratingly remains...

    I have a Z shaped object with 3 child box colliders triggers (box collides with trigger set to true). The Z shape is colliding with another mesh collider. I guess for reference the z shape has a rigid body but the 3 children do not. The 3 children contain the script to handle when they collide with another object. Here is where the problem comes up.

    The script is called 3 times, once for each of the child colliders that hit the mesh collider. Ideally the 3 children will act as one solid collider, so when any of them hit the mesh collider the script is called one time, and not one time for each of them. Anyone have any suggestions??

    Thanks!
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, if you've got three children with mesh colliders and no rigidbody, if the parent has a rigidbody all collision messages are handled as one and sent to the parent.

    So, put the script on the rigidbody parent and that should work.
     
    akuno likes this.
  3. swtoasty

    swtoasty

    Joined:
    Dec 10, 2008
    Posts:
    25
    All three children have a primitive collider. Can it work that way somehow? Or do they all have to have mesh colliders?

    Also, not sure if it matters, but I'm not really trying to detect collisions per se, rather trying to detect when colliders enter other trigger colliders.

    EDIT: Tried connecting the script to the parent object(which has a rigidbody) but it didn't work. Without the scripts on the actual child colliders the parent doesn't seem to recognize when it makes contact with another collider.
     
  4. swtoasty

    swtoasty

    Joined:
    Dec 10, 2008
    Posts:
    25
    Essentially, is there a way to create a compound collider out of primitives, and have it act as only one when it comes in contact with another collider? Because if all of the colliders in the compound one have a script (in this case 3 colliders/ scripts) the OnTriggerEnter is called three times. Is there a way to make it so it's called only one time if any of the three should trigger it?
     
  5. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Are you mixing trigger and collider on the children? If you do that the parent rigidbody tends to ignore the messages from the triggers in my experience.

    It works just fine for me with that setup, so I don't know why it doesn't work for you....

    Edit: Oh, it also doesn't work if any of the children also have rigidbodies.
     
  6. swtoasty

    swtoasty

    Joined:
    Dec 10, 2008
    Posts:
    25
    I don't think I'm mixing anything up. But I suppose I could be misunderstanding and putting things where they aren't suppose to be.

    Parent - Rigidbody
    Child 1 - Box Collider, Trigger On, Collision Script
    Child 2 - Box Collider, Trigger On, Collision Script
    Child 3 - Box Collider, Trigger On, Collision Script

    When it comes in contact with another collider the script is ran, but it's ran three times. :( I appreciate all your help and I'm trying to tweak things like I think you're suggesting to get it working. I'm sure it's one tiny thing that I'm missing :(
     
  7. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    I'm not completely sure the parent technique works if the children are triggers. Try them as colliders with isTrigger = false; and with the function call being OnCollisionEnter(hit : Collision).

    Make sure the parent is the only one with the script.
     
  8. swtoasty

    swtoasty

    Joined:
    Dec 10, 2008
    Posts:
    25
    Thank you very much!! I think you might have just saved me! :)
     
  9. gdbjohnson

    gdbjohnson

    Joined:
    Dec 16, 2014
    Posts:
    40
    The fact that the OnTriggerXXX messages cascade up the object hierarchy is an important detail that is not in the documentation.

    If you want to stop the cascade, it looks like the cascade stops at the first encountered RigidBody.