Search Unity

OnTriggerEnter2D is called one frame too late in Unity 5 - works properly in Unity 4

Discussion in 'Physics' started by Raveler, May 18, 2016.

  1. Raveler

    Raveler

    Joined:
    Sep 24, 2012
    Posts:
    40
    I believe I have found a rather serious bug in the Unity 5 2D physics related to triggers - OnTriggerEnter2D seems to be called with one frame delay. I've already reported the bug but I'd like to hear your opinion on what's going on here.

    I discovered that when a Rigidbody2D with a Trigger2D attached to it goes through objects, OnTriggerEnter2D is called AFTER the object has moved another step in Unity 5. It is called at the correct moment in Unity 4.

    I've attached a small package that demonstrates this strange behaviour.

    In Unity 4, the test gives this result, which is what makes sense to me:

    FixedUpdateFrame 006: Bullet is now at (10.5000000000, 3.5000000000, 0.0000000000)
    FixedUpdateFrame 006: Bullet at (10.5000000000, -0.5000000000, 0.0000000000) hit floor at (11.0, -2.0, 0.0)
    FixedUpdateFrame 007: Bullet is now at (10.5000000000, -0.5000000000, 0.0000000000)


    When the bullet moves from 3.5 to -0.5, it passes over an object, and OnTriggerEnter2D is called.

    BUT on Unity 5, the following happens:

    FixedUpdateFrame 006: Bullet is now at (10.5000000000, 3.5000000000, 0.0000000000)
    FixedUpdateFrame 007: Bullet is now at (10.5000000000, -0.5000000000, 0.0000000000)
    FixedUpdateFrame 007: Bullet at (10.5000000000, -4.5000000000, 0.0000000000) hit floor at (12.0, -1.0, 0.0)


    Note that OnTriggerEnter IS called, but only after the object has moved from -0.5 to -4.5 - even though it should have already triggered when moving from 3.5 to -0.5. It is called one frame late!

    This really messes things up for fast-moving objects. I think I can do a workaround this issue by doing a raycast to prevent this delay, but it's an expensive operation to do each frame. Very annoying!
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    This issue has already been fixed in 5.3.4p6. I haven't checked but it's more than likely fixed in earlier patches as well (p4/p5).
     
  3. Raveler

    Raveler

    Joined:
    Sep 24, 2012
    Posts:
    40
    Very awesome. Thanks!