I have implemented pooling for my projectile, where it adds the projectile to a pool once it collides with the BobbleArena / Alien / Columns (Ch. 4, Page 108).
However, I found that if I stand close to the arena walls, that the method: OnCollisionEnter does not always trigger. I found out that this is because the mesh collider for the wall isn’t always being hit when the projectile hits the walls.
Thus, sometimes I have to wait until the projectile goes invisible (calling the OnBecomeInvisible - which could take up to 10 seconds depending on where I hit on the arena).
My questions are:
What is a reliable approach for making sure that OnCollisionEnter is called when the projectile doesn’t always hit the mesh collider?
Is there a way to check how far the projectile has travelled, and then pool the object based on the distance it has travelled?
Is there a more reliable way to improve the mesh collider so that OnCollisionEnter is always called, regardless of where you hit in the BobbleArena?
Typically I’ve run into collision issues when an object is moving too fast. In such cases, the rigidbody almost skips over the collider. This shouldn’t be a problem with the book. If the collision detection isn’t working all the time, you can select the rigidbody and increase the collision detection amount. Granted this will impact your game’s performance. You can read about it here:
You may want to debug where your objects are being launched from. You mention that the collision is inconsistent when standing close to the walls. It sounds like the launcher may be outside of the player’s capsule collider and passing through the arena wall. This means when the gun fires the bullet, the bullet is already past the mesh collider when it gains velocity. That’s where I’d start looking. Try expanding the player’s capsule collider, or add a collider to the gun so that there is always a buffer between the player and the walls.
But generally speaking, if you run into collision issues and you aren’t dealing with fast moving objects, then it’s probably your code or colliders.