Pooling bullet-objects and IgnoreCollision

I’m working on a twin-stick spaceship shooter that should support a large number of AI controlled ships shooting simultaniously. As part of optimizing and dealing with problems with shots going over or under 3D objects I decided to use 2D physics and collisions. Another optimizing decision was to use pooling for bullet-objects.

I have now run into the problem that I set those pooled objects to ignore collisions with the collider of the ship that fired them, as the ship’s turrets are often within the collider. But while the Physics3D Ignore collision worked well for this, I found that the Physics2D variant wasn’t working, and the shots still collided with the parent ship. Looking at the documentation, apparently Physics2D’s ignore collision is removed when an object is set to inactive, which is what happens all the time with pooled objects. This limitation is apparently exclusive to Physics2D, Physics3D doesn’t have it.

Does anyone know why this is, and if there’s some way to have the same functionality without having to set the ignorecollision every time you get an object from the pool? Or am I forced to redo the ignore collision every time I fire a pooled bullet, and won’t that have a performance effect when there’s over a hundred guns firing?

Use physics layers.

2 Likes

I don’t think that’ll work in this case. Each bullet needs to ignore the ship it was fired from, but not other ships. I don’t want ships able to shoot through allied ships. So I’m pretty sure I’d need a layer for each individual ship, and since the number of ships can vary… I don’t see this working. Or am I missing something?

I suppose another way would be to use Physics2D.IgnoreCollision in the bullet script’s OnEnable method. This way it can re-ignore collisions after being disabled.

That’s the option I’m using now. I don’t like it very much, so I was hoping there was another trick. I find it strange that Physics always remembers the IgnoreCollision but Physics2D doesn’t. But, so be it. Thanks for the help.