Search Unity

Compound colliders cause unwanted OnTriggerEnter2D events

Discussion in 'Physics' started by Olipool, Feb 8, 2015.

  1. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    Hello everyone,

    I'm very new to Unity, this is ideed my first post here, so bare with potential sillyness on my side :)

    My problem is as follows:
    I have a car as a sprite with 3 colliders to match the shape, one box and two circle colliders. Also a rigidbody2D component. Furthermore I got a sprite of a person with a circle collider around it set as trigger.
    • When the car enters the trigger, the gameobject of the person is stored in a variable "person" in the OnTriggerEnter2D function.
    • When the car stays in the triggerzone and stops (velocity of the rigidbody<0.1f), a message is printed to the screen, this is checked in the OnTriggerStay2D function. The message is printed via "person.ShowMessage()" if person!=null
    • When the car leaves the trigger, the person is set to null and the message is hidden
    This works fine with one single collider on the car but with multiple colliders I get the following issue:
    • The first collider enters the person
    • While the car is still moving, the next collider of the car enters the person
    • Still moving, the first collider exits the person
    • The car stops, with the second collider still overlapping the person, but since there was en exit of the first collider, the variable "person" is null
    • The OnTriggerStay2D fires continiously but deoas not display the message because it thinks, there is no person any more
    Question: is it possible to tweak this behavior? Ther should be no OnExit from one collider while there is still another collider overlapping the triggering object. Technically it is right, ONE collider does indeed exit, but not the gameobject as a whole...
    Is this question understandable?
    Thank you so much for any advice on this! :)
     
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    322
    What I did now is counting the TriggerEnter events and in the TriggerExit event I decrease that number. Only if this number is 0 then the desired action in OnTriggerExit is performed. A little extra care is needed if two persons are overlapping etc. but it seems like a stable solution. But I still got the feeling I'm overcomplicating things...