Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnTriggerEnter2D, OnTriggerExit2D -- Unity Bug Workaround

Discussion in '2D' started by Ceffa93, Jan 14, 2014.

  1. Ceffa93

    Ceffa93

    Joined:
    Dec 2, 2013
    Posts:
    46
    Hi every one,

    as probably some of you know, these two methods are broken.

    OnTriggerEnter2D is called every single frame, instead of the first one, while OnTriggerExit2D is never called.

    Some of you has a solution? some idea to solve this, or maybe way to fix it that i don't know?


    ps: Some people think that the problem is the animator, but i disabled it and the problem is still there
    pps: Don't answer something like "You have to use collider2D instead of collider", i know that^^
     
  2. TaewYn

    TaewYn

    Joined:
    Jan 30, 2013
    Posts:
    19
    Hm.. while there definitely is a bug with OnTriggerEnter2D being called more than once, OnTriggerExit2D works fine for me.
    My workaround for OnTriggerEnter2D is to keep a list of the GameObjects that have already been triggered until they exit.
     
  3. negative_zero

    negative_zero

    Joined:
    Nov 14, 2013
    Posts:
    24
    This exact same thing happened to me. I just create a "canenter" boolean which upon the first entry gets shut off so subsequent entries can be ignored. Once you get an exit it gets turned back on again.

    Exit is a bit hairier. One thing I've done is ignore OnTriggerExit2D completely (because it never seems to work consistently for me) and instead create a boolean variable "hasexited" or whatever and in LateUpdate set it to true while OnTriggerStay (which I believe will always execute before Update) sets it to false again, thus as far as Update is concerned things should be accurate. I think. It seems to work, anyway, but it's very hacky and I'd recommend finding a better solution...
     
  4. Ceffa93

    Ceffa93

    Joined:
    Dec 2, 2013
    Posts:
    46
    negative_zero i thougth the same thing, and i thik now i'm gonna implement it, but thats absurd -.-
    now the question is:
    having all these trigger methods being called every frame, is, in terms of performances, better then having old good 3d colliders?
    You set their Z value to an high number, so that they act exactly like 2d colliders, and maybe, at this point, they are better
     
  5. DrKucho

    DrKucho

    Joined:
    Oct 14, 2013
    Posts:
    140
    I'm having similar issues here, but only when i use the 2Dtool kit custom box collider on each frame of the sprite , are you guys using 2D tool kit?
     
  6. Ceffa93

    Ceffa93

    Joined:
    Dec 2, 2013
    Posts:
    46
    nope, i'm using an empty object with a circe collider. The empty object has an animator that animates all of his children.
    I thought that the animator was the problem, especially because i knew for certain that it worked since a few days ago....but removing it changes nothing