Search Unity

Collider Issues

Discussion in 'Physics' started by Zullar, Jan 13, 2017.

  1. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    I am working on an ARPG


    But I'm struggling with colliders. 4 main issues/bugs.

    1: OnCollsionExit/OnTriggerExit not called when disabled/destroyed
    https://forum.unity3d.com/threads/ontriggerexit-oncollisionexit-and-destroyed-gameobjects.381145/
    https://fogbugz.unity3d.com/default.asp?859523_fduaelfpdjp731tq

    2: Cannot spot-check collisions with new created collider. For example if I wanted to check to see what is within a cone shaped collider I cannot instantiate the collider and then access collisions. I must instantiate collider and then wait until the next FixedUpdate to check for collisions. This adds a delay which makes coding some things a real pain. Raycast/spherecase are available but unity doesn't handle other shapes.

    3: Cannot access a list of current collisions. Many times I'd like to have a list of collider.currentCollisions but this doesn't exit. And if you attempt to maintain a list then bug #1 really makes it difficult.

    4: When toggling a collider from solid (non-trigger) to a trigger it calls OnTriggerEnter first, then OnCollisionExit. This can be a bit misleading. For example
    -Two colliders collide: OnCollisionEnter is called
    -One collider is toggled to a trigger
    -OnTriggerEnter is called (order should be backwards from this?)
    -OnCollisionExit is called (order should be backwards from this?)
    The order is correct according to https://docs.unity3d.com/Manual/ExecutionOrder.html, but how can you have 2 Enter's in a row without an exit?

    5: Hierarchy issues. Collisions/triggers automatically trickle down to the rigidbody. There is no easy way to distinguish what collided/triggered. My object is structured like this
    ObjectRoot: Rigidbody w/ Collider
    -ObjectChild: TriggerCollider aura

    When ObjectChild triggers with something it invokes OnTriggerEnter on any ObjectChild's mono scripts, but it ALSO trickles down and invokes OnTriggerEnter on any ObjectRoot's mono scripts. If ObjectRoot hits another trigger collider then it will invoke OnTriggerEnter on any ObjectRoot's mono scripts. In otherwords any scripts on ObjectRoot will have OnTriggerEnter invoked when anything in their layer OR child layers is triggered. So it's impossible to distinguish what hit when OnTriggerEnter was invoked (was it ObjectRoot or ObjectChild?)

    I'm not sure how to structure the hierarchy or where to place the rigidbody(s) so I can tell what was collided/triggered.

    I wish there was a collider.onTriggerEnter.AddListener method to avoid this.

    Has anybody else ran into any of these issues/bugs. Any advice/tricks would be appreciated.
     
    Last edited: Jan 21, 2017
    LuLusg and Cynicat like this.
  2. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    At this point I've created a ColliderWatcher script that I attach to all colliders. It records Enter/Exit and maintains a list of current collisions. However, it also handles OnDisable/OnDestroy and calls DisableExit for each current trigger/collision so there are never "dangling" collisions where Exit is failed to be called when an object is destroyed or scene is exited.

    It ended up being complicated to script (due to the way Unity's collision engine handles complex hierarchies and everything trickles down to the rigidbody... conflating all triggers/collisions). It's also tricky to handle how to disable colliders *during* a collision physics update (basically all collisions must be sorted and Enter's called first, then Exit's... never Exit before Enter)

    You can subscribe by ColliderWatcher.AddListener(TriggerCollisionData) which contains
    -Enum Trigger or Collision
    -Enum Enter, Exit, or ExitDisable (Enter and Exit are like normal, but ExitDisable is new and different and called when one of the 2 contacting colliders is disabled.)
    -Collision information (if it's a collision not a trigger)
    -Self Collider
    -Other Collider

    This solves issues #1, #3, #5 (It could also solve #4 with some sorting work)

    I'll keep testing and if it works OK I'll post here.
     
    Cynicat likes this.
  3. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    These are still issues for me. Is there any solution to these? Thanks in advance!