Search Unity

OnTriggerEnter2D / OnTriggerExit2D clarification

Discussion in 'Physics' started by dotsquid, Jul 18, 2017.

  1. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Hi there.
    Instead of a long verbal description I prepared a little scene and a big gif :)

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Trigger : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private int _counter;
    7.  
    8.     private void OnTriggerEnter2D(Collider2D collision)
    9.     {
    10.         _counter += 1;
    11.     }
    12.  
    13.     private void OnTriggerExit2D(Collider2D collision)
    14.     {
    15.         _counter -= 1;
    16.     }
    17. }
    As you can see I have a polygon collider which is a trigger and I count entries and exits of another collider. Somehow it appears that eventually the number of entries exceeds the number of exits.
    I wonder why Unity triggers yet another enter of the collider if it's already within?
    Summoning @MelvMay in thread :)
     

    Attached Files:

  2. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
  3. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    I won't let my question die in this abyss. So, bump.
     
  4. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Something tells me that @MelvMay is on vacation and that's the reason why he's keeping silence since the end of July.
    Anyway I bump this thread just in case.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
  6. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Ok, thanks. I'll send the scene I used to capture the gif with a bug report.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Thanks. Might want to mention this thread in your bug report so that QA don't just close it as a duplicate.
     
  8. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    well.. this "anomaly" is in unity since version 4.5 or even earlier. I know because it made me crazy and the development of one of my games to hell.. With every new unity version I check if it's fixed.. Continuous instead discrete tracking might help, but this will still happen in rare occasions.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Note that this has been fixed and is being put in all versions back to 5.6 soon.
     
  10. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Thanks. Now I can get rid of my workaround.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    It will land in 5.6.4p1, 2017.1.2p2 and 2017.2 (first patch).