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

OnCollisionEnter() not always firing?

Discussion in 'iOS and tvOS' started by Lostlogic, Oct 4, 2009.

  1. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    Is it normal for OnCollisionEnter() to not register sometimes? I have my objects change color and output debug text when the OnCollisionEnter() function is called and I have noticed that the system misses about 25-50% of the calls. It is never called even though I can see the objects react to the collisions (they are knocking around via physics.)

    Is there anything I can do to have the event work 100% of the time?
     
  2. neo

    neo

    Joined:
    Apr 22, 2009
    Posts:
    167
    I believe the physics engine does collisions at a certain time in each frame. IF one object moves thru another in the time between frames, then it will not be detected. So, when the collider or objects are too small, relative to their speed, they wont collide because one frame they haven't met yet and the next frame they have passed each other.
     
  3. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    Here's the thing though, the physics engine is working, it's just that my game isn't receiving the OnCollisionEnter() events. I can see the objects collide and react accordingly on-screen, but my event isn't getting executed. Is that to be expected?
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    is the object set to be a trigger?
    is the performance high enough? I guess otherwise the event could be removed from the queue again if it falls "too far behind" until it would be executed.
     
  5. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    It's not a trigger. I will try more solver iterations and see if that helps. The odd thing is that the physics system seeme to be working fine, it's just the event pushing that is missing calls or not sending them. That's what it seems like though.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, I've never seen that happen. It would make games using physics basically impossible if that were true.

    --Eric
     
  7. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    Hmm, I wonder if its another problem of Snow Leopard....I've seen quite a few issues so far so it wouldn't suprise me.
     
  8. ivanmoen

    ivanmoen

    Joined:
    Dec 26, 2008
    Posts:
    102
    Do you move the colliders around without adding a rigidbody?
     
  9. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    Sure don't. Nothing changes on the objects except internal vars I have (such as score.) I do have a FixedUpdate() routine present as well.

    Could the collisions be registering in there maybe? So far it's very annoying. My game is supposed to be over for example when the objects hit a certain collider but right now it registers about 50% of the time. The objects collide properly visually but no event is triggered.

    I've moved the solver iterations around (up and down) to no avail.
     
  10. ivanmoen

    ivanmoen

    Joined:
    Dec 26, 2008
    Posts:
    102
    And the object that hit the colliders has a rigidbody attached right?

    I’m using OnCollisionEnter and OnTriggerEnter all the time and I’m not experiencing any problems. Maybe you should create a small test project showing the bug and submit it with a bug report.
     
  11. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    Yeah, there is a rigidbody. Like I said before, it works about half of the time. I would assume it would work 0% of the time if I didn't have it set up properly?

    One thing I did was change the Physics FPS rate. Trying that now.
     
  12. HanulTech

    HanulTech

    Joined:
    Apr 5, 2009
    Posts:
    312
    How fast are the rigidbodies moving? As discussed on another thread here, there are velocities beyond which collisions will not be detected. You can see this by setting up simple scene where you bounce a ball off a wall. Things work fine up to a point, but there is a velocity threshold beyond which the ball will go right through the wall every time. This can be moderated a bit more by using a cube for the wall rather than a plane, but the problem occurs at some point just the same and just as consistently.

    The good news is that the problem only occurs at unrealistic velocity levels. I have found that you can stay well below this threshold and still have things look good. The thread where this was discussed was, if I recall, started by someone who was making a baseball game and was running into problems where home run balls hitting a collider were not having their collisions registered. You should take a look at that discussion.

    My suspicion as to the cause is that there is a time based threshold in the physics engine for collisions and that if a collision is not registered for at least that much time, it is ignored. At high velocities, I suspect that the collisions are failing to meet that threshold requirement. Either that or it is just a bug :D
     
  13. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    Things are moving quite slowly actually. They "look" fine and are reacting to the collisions. I'm just not receiving an OnCollisionEvent() call.
     
  14. Danneman

    Danneman

    Joined:
    Sep 29, 2009
    Posts:
    190
    Have you tried varying the size of the Box Collider?

    According to FPS-tutorial 2 the Box Collider needs to be bigger than the actual object itself - otherwise this exact behaviour youre describing can emerge.

    For instance, when I set the z-value to a too low number on a projectile used by a weapon, it will penetrate any object. Raise the value, and it will bounce.


    ..but as you say, stuff reacts the way they should, so this is probably not your issue.
     
  15. 8bitRick

    8bitRick

    Joined:
    Nov 18, 2013
    Posts:
    11
    I know this thread is SUPER old but I'm seeing this same behavior in Unity 5.4.1p1. My setup is complex but essentially I see the same issue. I see the bounce happen fine (physics works) but OnCollisionEnter is not called sometimes. This happens randomly like 1 in 50 times. @Lostlogic did you ever figure this out? I've been googling around have not seen any other helpful threads or blogs.
     
  16. Lostlogic

    Lostlogic

    Joined:
    Sep 6, 2009
    Posts:
    693
    I believe I set the physics sample rate up to get it to work. That was a long time ago though and unfortunately I'm not exactly sure what ended up happening.
     
  17. 8bitRick

    8bitRick

    Joined:
    Nov 18, 2013
    Posts:
    11
    Cool. I ended up increasing "Default Contact Offset" by a factor of 10 (to 0.1), since my world is scaled up by 10 for other physics reasons. It "feels" like things are better now. I think I've still seen a missed collision but can't be 100% sure since my game is pretty active. Thanks for the response!
     
  18. CleverNickname

    CleverNickname

    Joined:
    Aug 18, 2015
    Posts:
    16
    I've been fighting with this same bug for awhile now, through 5.3 and now into the latest release of 5.5. It only happens with continuous dynamic collisions, never discrete ones (though unfortunately I can't reliably use discrete collisions with high-velocity projectiles). Maybe once in every 20 or 30 collisions, a collision will happen without actually calling the OnCollisionEnter event. I've got a laundry list of potential fixes to try tomorrow, I'll report back if I can get any of them to work.
     
  19. ynaoto

    ynaoto

    Joined:
    Mar 26, 2016
    Posts:
    21
    I have exactly same issue with continuous dynamic collisions. I am truly looking forward your report!
     
  20. CleverNickname

    CleverNickname

    Joined:
    Aug 18, 2015
    Posts:
    16
    Unfortunately I don't have any good news to report. I fiddled with absolutely every collider, rigidbody, and physics setting I could think of, and the problem still persisted. I ended up rolling my own solution using raycasting to detect when a collision should have occurred but didn't, and then handling it from there based on an approximation of what the collision data should have been.
     
  21. ynaoto

    ynaoto

    Joined:
    Mar 26, 2016
    Posts:
    21
    Thanks for sharing your result. The phenomena is still exists even with other Interpolate and Collision Detection modes in my project. I am trying to make simple project to reproduce the problem, but no success yet. I am going to ask Unity support if I successfully got the sample project.
     
  22. oliran

    oliran

    Joined:
    Sep 29, 2015
    Posts:
    49
    I have been struggling with this problem for a long time, but finally found the one thing that so far seems to work. I added OnCollisionStay(Collision collision) which actually gets called even when OnCollisionEnter doesn't. Why? No idea. But it does seem to work!

    For the code, I do this to catch whatever is thrown at it:

    Code (CSharp):
    1.     private void OnCollisionEnter(Collision collision)
    2.     {
    3.         Collided(collision.collider, collision);
    4.     }
    5.  
    6.     private void OnTriggerEnter(Collider other)
    7.     {
    8.         Collided(other);
    9.     }
    10.  
    11.     private void OnTriggerExit(Collider other)
    12.     {
    13.         Collided(other);
    14.     }
    15.  
    16.     private void OnCollisionExit(Collision collision)
    17.     {
    18.         Collided(collision.collider, collision);
    19.     }
    20.  
    21.     private void OnCollisionStay(Collision collision)
    22.     {
    23.         Collided(collision.collider, collision);
    24.     }
    25.  
    26.     private void Collided(Collider other, Collision collision = null)
    27.     {
    28. // Stuff
    29.     }
     
  23. madfatcat

    madfatcat

    Joined:
    Jul 3, 2014
    Posts:
    14
    Let me resurrect this thread. I experience the same issue in Unity 2017.3. The difference with situations described above is that my dynamic object with RigidBody2D collision mode is discrete and OnCollisionStay2D is not fired as well. Details: a plane drops a bomb on a building. In 95% of cases collision between the bomb and the building is registered properly, but sometimes, when the collision angle is small or bomb speed is small, both OnCollisionEnter2D and OnCollisionStay2D are not fired. Collision is processed by the engine cause bomb lies on the building and doesn't fly through it.
     
  24. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Ah.. I was having a weird issue where most of the time, my collision would register OnCollisionEnter messages, but like 15% of the time, it just wouldn't happen. After a day of being unable to rip out my already short hair, it turns out my sword's collider didn't extend enough into the handle, so sometimes it could go right pass my enemies' hurtbox colliders..

    This was mostly problematic for me because I'm mixing animation with enabling/disabling colliders and checking if they hit stuff.

    So especially if you're animating colliders:
    Lesson of the day: I know it's simple but.. are your colliders big enough? ^_^" Lol
     
  25. Soogbad

    Soogbad

    Joined:
    Apr 22, 2017
    Posts:
    8
    This post was really helpful but for me changing the Default Contact Offset in Edit>Project Settings>Physics from 0.01 to 0.1 wasn't enough, and with 0.25 the projectile made collision with the enemy from a too far away distance. So what made it from me is from there to also set Contacts Generation to Legacy Contacts Generation instead of Persistent Contact Manifold
     
  26. _yash_kaushik_

    _yash_kaushik_

    Joined:
    Jun 21, 2015
    Posts:
    1
    Hi, I had this same issue for a very long time and for me a simple tweak fixed this. I changed the Sleeping Mode of my rigidbody2d to 'Never Sleep'. Unity sometimes put the rigidbodies to sleep(put it out of the physics simulation temporarily) to save computation. By making it 'Never Sleep' helps prevent that and after that tweak all my OnCollisionStay2D are getting registered properly. If the issue still arrives in my projject i will post an update here.

    (Update)
    Nevermind I am still having those issues of collisionstay not firing. Sorry!

    (Update #2)
    Ok So in my case just using OnCollisionEnter2D instead of OnCollisionStay2D worked because the former gets called earlier than the latter. Also in my game objects always bounce back after collision immediately so maybe this was making OnCollisionStay2D not work like expected. But OnCollisionEnter2D is working completely fine right now. Lets hope this does not create any complications in the future because at this point i do not even know why i used OnCollisionStay2D 2 years ago when i started writing this game :)
     
    Last edited: Jan 16, 2021