Search Unity

OnTriggerEnter2D stopped working.

Discussion in 'Physics' started by BugKiller, Dec 9, 2015.

  1. BugKiller

    BugKiller

    Joined:
    May 8, 2014
    Posts:
    23
    Hello,
    i'm using Version 5.2.3p2 and OnTriggerEnter2D(Collider 2D) is not working with Collider2D, PolygonCollider2D, BoxCollider2D nor CircleCollider2D. I can check the objects are colliding with a simple print to the console on OnTriggerStay2D() but the collision is not triggered when it starts.

    The weird thing is that sometimes it works and others it doesn't. Another important thing is that I didn't change any of the objects' properties or code for more than one year and it stopped working with the last Unity updates.

    This is my code:

    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D otherCol)
    2.     {
    3.         var enemy = otherCol.GetComponent<Enemy>();
    4.         if (enemy == null)
    5.         {
    6.             //Debug.Log("enter field no enemy component " + otherCol.gameObject.name);
    7.             return;
    8.         }
    9.  
    10.         enemy.EnteredField();
    11.     }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    You are saying you that NEVER receive an OnTriggerEnter2D but do receive a OnTriggerStay2D. I've not seen this reported before.

    Or are you trying to say that you don't get the OnTriggerEnter2D when you can see colliders overlapping but instead, get it a single physics update later?

    Either way, I won't be able to help you easily without a bug report that has a simple reproduction case. Give me that and I can investigate immediately.
     
  3. dakka

    dakka

    Joined:
    Jun 25, 2010
    Posts:
    113
    I had the same problem. The work around for me was adding a rigidbody2D, set kinematic to true. Then triggers worked.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    How can you know it's the same problem? I am still waiting on actual bug reports myself. If you could enlighten me on the set-up then I could understand what is happening myself.

    Are you saying you were moving static colliders (which you're not supposed to do) and were expecting them to collide and you had to (correctly) use a Kinematic Rigidbody2D to get it to work?
     
  5. dakka

    dakka

    Joined:
    Jun 25, 2010
    Posts:
    113
    Sure, some more detail. After I updated to the patch release v5.2.3p2 my animating GameObject with 2d collider triggers stopped working. I found I needed to add a rigibody2d to make the triggers work. And marked it kinematic since I was animating the object and not using physics.

    And was the same problem, sometimes it would trigger, next run it wouldn't.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    Okay, I think I understand what is going on. This is definitely related to moving static colliders. The problem here is as I've described in another thread; you should not move static colliders.

    In fixing another issue, this behaviour has changed. I am making a change now to ensure that behaviour is reverted. In the meantime, I would highly recommend that you don't move static colliders.
     
  7. dakka

    dakka

    Joined:
    Jun 25, 2010
    Posts:
    113
    @MelvMay Thanks for the update. It seemed like I had an incorrect setup to start with, then needed to update to the correct method.
     
  8. BugKiller

    BugKiller

    Joined:
    May 8, 2014
    Posts:
    23
    I am saying that sometimes I never receive one OnTriggerEnter although I receive OnTriggerStay every frame and other times I do receive one OntTriggerEnter followed by OnTriggerStay every frame. I tried with older versions of Unity such as 5.2.2p3 and 5.2.2p4 and it worked fine as in the past did. I think this problem just started with the version 5.2.3.

    I read what you wrote on the other thread

    ... about manipulating the Transform of the objects not "activating" the physics system and it makes sense to me, but that means I must change all the old code I used to move objects in all my project..

    As you said:

    and if I understood it well, If I wait for your update/fix I don't need to change all the code on my project because it will work again??
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    Maybe that's being affected by what I found but I'm not entirely sure. If there's a way you could create a simple reproduction case for this and submit a bug then I can confirm immediately if this will be fixed or if it's another issue entirely.
     
  10. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    @MelvMay Hey, I just wanted to ask something which seems related to this since updating to 5.3 some of my Physics2D checks seem to be malfunctioning.

    If I use Transform.Translate to move my character which has a BoxCollider2D and a Kinematic Rigidbody2D, does this mess up some Physics2D stuff with the new updates? It worked fine in 5.2.2, but I'm wondering if there was a change in the new update related to this? Is there any method that I could call on the RigidBody2D to fix it after I do my Transform.Translate? Or do I have to use RigidBody MovePosition or set the velocity.

    Cheers
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    Everything that is in 5.3 is in 5.2.3 patch. 5.3 contains 4 new joints and a new effector but it's the same thing. Maybe something changed during 5.2.3 but I can't answer your question, I don't know what 'mess up' means. Certainly, MovePosition is always recommended. You should always avoid modifying the transform when using physics components.

    Creating a bug report with a simple reproduction case is trivial to do. Do this and I can give exact answers and provide any fixes and/or workarounds if there's a problem.
     
  12. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    hi @MelvMay

    I'm just wondering what the correct procedure is from now?

    Can I suggest 4 cases as I would normally go about making:
    1. Door. Collider on door sprite, set to trigger, script using OnTriggerEnter2D
    2. Animated tentacle. Collider on each segement, each segement is animated from a root object. Player is hurt when entering this collider using OnCollisionEnter2D and layers or tags to tell what layer the tenticle colliders are on.
    3. Bullet. Collider on bullet, script on bullet that removes bullet with OnCollisionEnter2D
    4. Left to Right enemy. Driven by Update, the enemy is moved by transform.Translate and has a Collider attached.
    Case 1,2 & 4 don't use RigidBody2D

    Currently 2 & 3/4 in Unity 5.3 don't make the bullet disappear if it hits the colliders.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    Sorry but describing set-ups like this (which have missing pieces of information) isn't helping much.

    There is no intended behavioral change so I'm not going to start recommending a new way of doing things. If you have a problem then simply submit a bug report with a simple reproduction project and give me the case number so I can investigate.

    Sorry if I sound abrupt here (I'm genuinely trying to help) but I spent most of yesterday typing on the forums with only two bug reports to show for it.

    Bug Report! Please!
     
  14. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    @MelvMay I've made a quick demo setup and submitted as a bug, hope it helps
     
    Last edited: Dec 10, 2015
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    Thank you, that would be awesome.
     
  16. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    seems the bug reporter doesn't email us anymore (can't get case number) so it's in there somewhere! ;) I set up a collision demo
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    Maybe it's just being slow. Could you give me the title you used so I can search for it?
     
  18. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,457
    Yeah, that's not correct, it's not a duplicate of the case you mentioned and should not have been marked as such. Not sure why QA thought that but with so many cases, mistakes happen.

    The good news is that your case is indeed the same as the ones others are reporting and is covered by the fix I did the other day. I'll still mark the case as a duplicate but against the correct case.

    Thank you very much for the bug report; it adds confidence to the fix already in progress. :)