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

OnTriggerExit2D not working when changing local scale

Discussion in '2D' started by fmarkus, Nov 16, 2013.

  1. fmarkus

    fmarkus

    Joined:
    May 22, 2010
    Posts:
    181
    Hi,

    It seems that the 2D collision system will not generate an OnTriggerExit2D if the local scale of an object changes while the collision is happening...
    I have to exit the collision, re-enter it and THEN an OnTriggerExit2D event will happen.
    Are you aware of that bug and is the a work around or do I need to do something special?
    Thanks!
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    When the local scale is changed during the collision, is the collider still also resized with it? Is it still within the bounds of a collision? I used the following script on a gameobject and the only side effect was that when the object was scaled down it generated another OnTriggerEnter event but still generated the Exit as well.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TriggerTest : MonoBehaviour {
    6.  
    7.     private bool scaled = false;
    8.  
    9.     void OnCollisionEnter2D(Collision2D col) {
    10.         Debug.Log("COLLISION ENTER");
    11.     }
    12.  
    13.     void OnCollisionExit2D(Collision2D col) {
    14.         Debug.Log("COLLISION EXIT");
    15.     }
    16.  
    17.     void OnTriggerEnter2D(Collider2D col) {
    18.         Debug.Log("TRIGGER ENTER");
    19.         if (!scaled) {
    20.             transform.localScale = new Vector2(0.7f, 0.7f);
    21.             scaled = true;
    22.         }
    23.     }
    24.  
    25.     void OnTriggerExit2D(Collider2D col) {
    26.         Debug.Log("TRIGGER EXIT");
    27.     }
    28. }
    29.  
    30.  
     
  3. fmarkus

    fmarkus

    Joined:
    May 22, 2010
    Posts:
    181
    Hi!

    Well, I update the local scale of object 1 in my Update loop when Object 2 is in collision with it. So that works and physics collisions are happening because the object 1 scaling is pushing all objects around it. Object 2 stays at the center of object 1 and doesn't move until I move it away.
    But I get not OnTriggerExit event :(
     
  4. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Hmm, I can't really say anything else without seeing what is happening or looking at how it's being done via code. Please share something on here that can help the community diagnose your problem. The issue honestly sounds like a matter of implementation and not a bug.
     
  5. fmarkus

    fmarkus

    Joined:
    May 22, 2010
    Posts:
    181
    My trigger is following the mouse, so I can do rapid movements and exit the collider very quickly. I don't know if your test can do that.
    I have removed everything and I can see that if I scale in the OnTriggerStay2D loop or in Update or FixedUpdate, or LateUpdate, the OnTriggerExit2D Debug.Log I have there will not display every now and then. It is missing the event.
    If I do NOT scale and move the trigger in and out quickly, I get 100% Exit messages...
    There is something going on here...
    Can you make a test and tell me if you get the same with a trigger linked to your mouse?
    Cheers.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    By changing the transform you are essentially warping the collider through space which, for the same reason you shouldn't directly position a rigid-body, causes contacts to be regenerated. Any existing contacts will just be replaced by new ones at the new position.

    Unfortunately, Box2D does not provide the ability to perform arbitrary transformations (position, rotation or scaling) on colliders resulting in the need to regenerate them internally. This causes Box2D to destroy any existing contacts on the collider (potentially many internal fixtures in the case of a polygon collider) and regenerate new ones.

    This doesn't work well for how we expose these as completely mutable component inside Unity and essentially it causes new contacts to be produced.

    We are looking at how we can at least correlate contacts prior to a rescale/transformation-change with new incoming contacts caused by the internal regeneration.

    Hope this helps.
     
  7. fmarkus

    fmarkus

    Joined:
    May 22, 2010
    Posts:
    181
    Thanks a lot MelvMay!
    That explains it. I didn't have that problem with Chipmunk physics, as I could call a Chipmunk.Update when I changed the transform, without a loss in performance.
    So I'm back to Chipmunk. I hope you guys can solve this issue.
    Thanks again for taking the time to look at this.

    F.
     
  8. CineTek

    CineTek

    Joined:
    Jul 12, 2013
    Posts:
    98
    I am currently working on my Ludum Dare 28 project and experience the same problem.

    My weapon should determine with OnTriggerEnter2D/Exit2D if an enemy is in range. It´s attached to my player object and he is scaled by using an Vector3 and the localScale value.

    Any news about this? Has someone found a solution (that works in my setup) to this problem with basic Unity Box2D physics? (no third party engines likle chipmunk)
     
  9. fmarkus

    fmarkus

    Joined:
    May 22, 2010
    Posts:
    181
    Any update on this?
     
  10. yurilin1

    yurilin1

    Joined:
    May 22, 2013
    Posts:
    102
    attach a 2d collider rigidbody2d as a parent without scale and make scale in the children.
    (tested,still not fire exit2d,but maybe you should work)
     
  11. Dorian75

    Dorian75

    Joined:
    Oct 2, 2013
    Posts:
    2
    I got the same problem
    I desactivated the 2d box collider in animation, or I tried to move it by moving it center in animation, but it doesn't fire OnTriggerExit2D...

    In these conditions 2D colliders are useless... hope it will be fixed soon

    (And I can't make OnTriggerStay2D works properly)
     
    Last edited: Jan 17, 2014
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    I have implemented the contact correlation for 2D physics and it has been released to an internal alpha build.

    This essentially deals with the fact that Box2D re-generates contact information and looks at the surfaced collision/trigger state and ensures that you continue to get "Stay" conditions even if the contact was regenerated due to (say) the collider being moved or the internal Box2D fixture being recreated.

    Previously you'd not see the "Stay" callback but instead just see a new "Enter" callback. Also, the algorithm will ensure that if you were in a "Enter" or "Stay" callback and due to a move/regeneration you'd then get the "Exit" callback.

    In short; what you see on the Unity end for callbacks will work as expected no matter what we're forced to do because of the way Box2D works.
     
  13. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    That's awesome to hear! Will definitely help simplify my item pickup code a bit now :)
     
  14. gapMindful

    gapMindful

    Joined:
    Jun 26, 2013
    Posts:
    17
    Any estimate of when this fix might be available? My game objects might not always be symmetrical so re-scaling them but not their colliders like suggested isn't a very scalable fix for me.
     
  15. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    Just remeber OnTriggerEnter2D and OnTriggerExit2D REQUIRES rigidbody2D to work!
     
  16. twoski

    twoski

    Joined:
    Mar 27, 2014
    Posts:
    27
    This is great news, when will these changes be made official?
     
  17. swinemerchant

    swinemerchant

    Joined:
    Apr 21, 2014
    Posts:
    4
    Hi,

    I'm still unable to fire OnTriggerExit2D, having seen this thread maybe it isn't my code, has this been fixed?

    many thanks,
     
  18. AngryCSharpNerd

    AngryCSharpNerd

    Joined:
    Jul 11, 2012
    Posts:
    18
    Not just with OnTriggerExit, but I'm having trouble with OnCollisionStay2D was well.

    I'm having a 2D enemy move side-by-side by multiplying the localscale by -1, and when it's changed it will not work.
     
  19. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Unfortunately, it's because there still hasn't been an update. This is half a year after the 2D system came out, and yet still no bug fix update. I'm very disappointed in my experience with Unity due to this. Other popular Engines try to make a case of at least an update a month, which is more than reasonable. I hope the Unity team reworks their update pipeline to be more inline with their increased popularity... Pushing all these bug fixes into an incredibly later release that requires it to wait for a bunch of new features they are working on, is quite silly.

    Or at the very least, make Beta's public, letting people choose if they want to risk any other new bugs.
     
  20. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,605
    If they've got a fix in the works for ANY upcoming version, that's still way better than their record on Unity's horribly broken joystick support. It's been 5 years and counting on that issue for me.

    My advice: Don't wait for them to fix things. Find a way to work around the problems or you may end up waiting forever.
     
    Last edited: May 28, 2014
  21. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Oh of course, there is almost always some sort of workaround and that is what I had to do. The bad thing though is that nearly every time, a workaround is going to be less efficient and is going to make code more complicated than it needs to be. It's just really sad to see this kind of release schedule from a company that is really huge in the game dev scene now.
     
  22. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,605
    Agreed.

    However, you may be in luck this time. It looks like significant changes were made in the 4.5 release today. I haven't done any testing, but here are some entries in the release notes that may be related:

    The 2D system is getting a ton of attention now as it seems like at least half of the 450 bug fixes they're touting in the 4.5 release are for 2D. I wish they'd give some attention to the old, forgotten, but critical systems like Input. Sigh.
     
    Last edited: May 27, 2014
  23. Xinman100

    Xinman100

    Joined:
    Feb 17, 2014
    Posts:
    1
    I had this problem,too. Finally I found a method. When Getting triggerenter I made the collider radius +=0.1f (circle collider 2D),then I solve this problem. Maybe it is not safe.
     
  24. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    Hm I'm still not getting any calls to OnTriggerExit2D. Any words about this?
     
  25. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Not sure why you quoted something that happened January last year!
     
  26. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    I found this topic too, to find info about why when I disable a gameobject (and its 2d collider) the callback OnTriggerExit2D is never called. We have to reference the trigger to the object inside or to teleport the object to get a proper behavior.
     
  27. Exvel

    Exvel

    Joined:
    Jul 7, 2013
    Posts:
    2
    Year 2016
    The bug is still not fixed
     
  28. Exvel

    Exvel

    Joined:
    Jul 7, 2013
    Posts:
    2
    Just tried 5.3.1. It is now fixed. THANK YOU DEVS!