Search Unity

Collision giving bogus gameObject

Discussion in 'Scripting' started by podperson, Sep 8, 2008.

  1. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I've got a composite object (the idea is I want a vehicle with extra bits that can be blown off). Each piece has its own collider and they're arranged in a hierarchy, but when collisions happen the collision.gameObject is often (in some cases dependably) the parent object and not the actual object hit.

    I've even tried putting the child collider way off to the side, and it still reports that the parent object was hit.

    What am I doing wrong?
     
  2. galent

    galent

    Joined:
    Jan 7, 2008
    Posts:
    1,078
    Ok, I'm going to take a stab at this, based on this weekend's nightmare...

    I'm assuming that the parent game object is the only one with a rigidbody (if not please disregard everything else I write ;) ). My understanding is the message is sent to the collider the rigidbody, which in this case is the parent object (the collider's "rollup" based on the object with the rigidbody(s)). In order to do what you're describing you'd need to add rigidbodies to the child objects.

    Thanks,

    Galen
     
  3. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Interesting. You're right about the rigidbody in the parent and not the child. I *thought* I tried adding rigidbodies to the children but I may have missed one and decided I hadn't fixed the problem.
     
  4. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Let me just confirm that this worked.
     
  5. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Although it's working now, you could also try collision.collider.gameObject. As mentioned, collision.gameObject is basically collision.rigidbody.gameObject...

    We used a single rigidbody and multiple colliders for jeep damage in Raptor Safari. Glancing at the code now--we use thisCollider and otherCollider in ContactPoint.
     
  6. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Hmm I thought I tried collision.collider, but maybe I didn't. If so, it's preferable since I didn't WANT the extra rigidbodies in some cases.

    I did try contacts[0] and that did *not* work. Perhaps I need to spelunk the array more carefully :-/
     
  7. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I ran into this bug again and tried a bunch more things (I didn't feel like adding a bunch of rigidbodies this time):

    info.contacts[0].otherCollider works
    info.collider et al do not