OnCollisionEnter2d only being called sometimes
Hello,
I am having a strange problem that I have only seen mentioned once before.
Simply put; I have fast moving projectiles launched from the player which collide with enemies to encase them, both objects have RigidBody2D, the projectiles use CircleCollider2D and the enemies have a five sided PolygonCollider2D. Everything works exactly as it should, the objects collide with the enemies every time and bounce off. However the function OnCollisionEnter2D is only called about 1/3 to 1/4 of the time, even though the projectile can clearly be seen bouncing off the foe.
Even just a simple;
void OnCollisionEnter2D( Collision2D col ){
Debug.Log ("Collided with " + col.gameObject.name);
}
...only activates occaisionally, even when the projectile bounces off.
I attached a OnCollisionEnter2D to both the enemy and the projectile and they both activate at the same time, with the exactly the same response rate.
The projectiles are pooled and recycled, with Sleep/WakeUp and gameObject.SetActive being used to activate them when launched, but removing the Sleep and WakeUp seemed to make no difference.
Could it be with fast moving projectiles that if the collision enters and then exits almost immediately that OnCollisionEnter2d is not called? As stated before, it all works fine, but just not all the time.
Thanks for the help!
James.
Oh and to clarify, all objects are only ever moved with addForces, including launching the projectiles, which have a single large force to launch and then nothing else. Also the projectiles are scaled, increasing in size throughout their lifespan.
Hmm if I change the projectiles Collision detetion from discrete to continuous it massively increases the chance that OnCollision is called (allthough still not 100%). But I am still confused, surely if the rigidbodies collide, they will always call the collision function?
Your answer
![](https://koobas.hobune.stream/wayback/20220612055539im_/https://answers.unity.com/themes/thub/images/avi.jpg)