Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnCollisionStay2D is skipped sometimes

Discussion in '2D Experimental Preview' started by Jay-Pavlina, Jun 23, 2016.

  1. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    I have a script logging in FixedUpdate and in OnCollisionStay2D, with two colliders touching. Every so often, maybe every half a second, the OnCollisionStay2D method is not called, but FixedUpdate is still called correctly. The colliders have not moved, and no OnCollisionExit2D event is sent, so I know they are still touching.

    @MelvMay If you are unable to reproduce it, I can try to put together a repro project for you.

    Edit: It's a kinematic rigidbody with full kinematic contacts turned on, and the collision is happening between a BoxCollider and the polygon collider generated by the tile map.
     
    Last edited: Jun 23, 2016
    der_r likes this.
  2. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    [edit] Removed. I thought my problem was related, but it was about a different system (triggers).
     
    Last edited: Jun 23, 2016
  3. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    Just tried OnTriggerStay2D and it is working fine for me. I'm only experiencing the issue with OnCollisionStay2D.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,333
    Odd. Stay callbacks stop being sent when the bodies sleep so maybe it's that? If not, could you create a bug report with a simple reproduction case and I'll get it fixed.
     
  5. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    You were right; it was falling asleep. Here's a detailed explanation of what was happening.

    I had a kinematic rigidbody that I was manually applying gravity to in FixedUpdate, and in OnCollisionStay, I would position it on top of the ground if it was standing on something. It would land on the ground and stay there for a bit, then for one frame, it would fall asleep, the gravity would push it down, but OnCollisionStay would not get called, so it would not get positioned back on top of the ground. This movement would wake it up and in the next OnCollisionStay it would get positioned back on top of the ground.

    Thanks! I didn't even know kinematic rigidbodies could fall asleep. I'll be sure to set the ones that control player characters to always awake.
     
    MelvMay likes this.