Search Unity

Collision methods: List of contact points is mixed up/incorrect?

Discussion in 'Scripting' started by kromenak, Jul 22, 2011.

  1. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    Hey everyone,

    I'm working on some collision code and I've run into an issue which seems pretty big, but I'm not sure if I'm doing something wrong or if it is a bug or what.

    Basically, I have two objects: Object1 is a rigidbody made up of two colliders. Object2 is a rigidbody made up of one collider. When they collide, I want to check their contact points and use the normal to see if one is on top of the other. This is where things get hairy.

    When the two objects collide, Object 1 registers eight contacts, one for each corner of its two colliders. However, the "thisCollider" and "otherCollider" values seem inconsistent - sometimes "thisCollider" holds Object1's collider, sometimes it holds Object2's collider. It seems that in Object1's OnCollisionStay function, that "thisCollider" should always hold Object1's collider!

    This also seems to cause the "normal" value of the contact to be reported incorrectly - exactly the opposite direction that you expect it to be pointing in.

    Don't know if anyone else has run into this issue, but is there something I might be doing wrong to cause this? Or is it a bug in the way Unity is assigning thisCollider and otherCollider?
     
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    I think it might be a 2 collider problem. Can you post a screenshot. I don't get what's your thisCollider and otherCollider mean.
     
  3. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    I'm trying to work out what causes ContactPoint to swap this, because it still happens in Unity 4.2, 3½ years later.

    I have a situation where it always swaps:
    • I load my scene and spawn an object. I have an OnCollisionStay method on the terrain, and when the object hits it, ThisCollider and OtherCollider are the correct way around. i.e. thisCollider is the collider of the object receiving the OnCollision event.
    • I move to another scene. My spawned object has DontDestroyOnLoad set so it persists.
    • I reload the original scene. When the spawned object collides with the terrain, OtherCollider is now the terrain's collider, and the values stay swapped for further collisions.

    However, that's in my main project. When I repeat those steps in a separate test project, everything works perfectly, so there's something more that I'm doing somewhere that's triggering it but I'm not sure what it is. There's quite a lot going on so it's hard to strip down.

    Anyone else who's seeing this bug, maybe you can narrow it down further. I'd like to have a working example before submitting a bug report but it's proving more difficult than I'd hoped.