OnCollisionEnter occasionally misses

I have this simple script that detects when a ball is hitting an obstacle. The obstacle (where script is attached) would just let the ball pass through.

function OnCollisionEnter (collision : Collision) {
	for (var contact : ContactPoint in collision.contacts) {
		if (contact.otherCollider.tag == "Ball") {
			Physics.IgnoreCollision(contact.thisCollider, contact.otherCollider, true);
		}
	}
}

Occasionally, the OnCollisionEnter would fail to get triggered and the ball would bounce off the obstacle. Any idea why OnCollisionEnter doesn’t always get triggered?

I think OnCollisionEnter might still be getting triggered. You can print something to make sure.
What are you trying to do exactly?
Just so you know, in Unity 3 you have a collision matrix where you can specify whether different layers collide or not.

No, this is a natural PhysX issue, been an issue for a very very very long time. The faster the object while traveling, the higher the risk that the object collision might be missed. There is absolutely no guarantee that colliders will work, this gets exponentially worse the further from world center objects get. Also, the larger poly objects have a higher chance that smaller poly objects will pass through them. Has nothing to do with Unity, this is purly a PhysX issue.

the way I understood this post is that the author wants the ball to go through the obstacle (ignore collisions between the ball and the obstacle) but occasionally the ball bounces off. It might be because the ball is moving too fast so that it happens that the collision is indeed just not detected sometimes like you said. But it might also be a problem with Physics.IgnoreCollisions(). No need to set ignore collisions when the collision actually happens. Perhaps calling Physics.IgnoreCollisions() only once, when the ball is created would help.

Again, in unity 3, you will be able to put the ball in its own layer and the barrier in a different layer and ignore the collisions between the 2.

If the collision is not detected, how does it bounce then?

It’s obvious that the PhysX engine is aware of collision. And when it makes the ball bounce, it’s strange for the PhysX not to be able to callback the collision event function. Really wonder what kind of bug would cause this.

Here’s a possible scenario:

  1. collision is detected → ball bonces off
  2. collision is not detected → balls goes through
  3. code:
function OnCollisionEnter (collision : Collision) { 
   for (var contact : ContactPoint in collision.contacts) { 
      if (contact.otherCollider.tag == "Ball") { 
         Physics.IgnoreCollision(contact.thisCollider, contact.otherCollider, true); 
      } 
   } 
}

doesn’t do much.
Solution might be to move the Physics.IgnoreCollision outside of the OnCollisionEnter function and call it only once for the ball and the obstacte. Check documentation on IgnoreCollisions:

Wait for unity 3 and use layers.

What happens is actually an extension of your first entry :slight_smile:
collision is detected → ball bonces off → event is not triggered

According to me it’s unlikely for the PhsyX not to trigger the event while it detects collision and bounces the rigidbody.

May it be possible that Unity misses the event generated by PhysX?

I took ivkoni’s suggestion to move the IgnoreCollision call outside of the OnCollisionEnter event. The ignore collision call between ball and obstacle is now initialized in a Start event. That worked consistently. It’s still odd to me that sometimes the OnCollisionEnter event didn’t get triggered. I tried adding a debug statement there as well and it was in fact not being triggered when the ball bounced. The ball wasn’t even moving that fast. Thanks for the help!

Just an FYI, i get issues where collisionEnter wouldn’t get triggered too. Its happening for me when doing platforming code.

A faster fixedupdate rate will fix it. Trigger detection appears tied to it, regardless if you use it or not, otherwise you can extend it by half of the velocity as a quick hack.