Unity 3d Filtering collisions

A quick and simple tip, for when you just need storms of missiles!

When implementing logic to arbitrate collisions, things generally go okay initially, nothing too busy, a single bullet zips to its target and a collision happens… Great but what if we want a storm of bullets. The first thing you’ll notice if you create a spray of simultaneous bullets is that because they are all starting from the same place, even with a slightly different heading, they all immediately collide with each other. Of course you don’t have to have them destroy each other but what they will do is bounce off each other, suddenly your immaculate bullet fan is chaos!

As there only needs to be one rigid body involved in a collision, your thought might be to have bodiless bullets, with the targets having a body. This will look like its working, your fans of bullets will look good, but they will collide very unreliably. This is because the body that’s “seeing” the collision isn’t moving, so some collisions can get missed (a bullet can sometimes tunnel right through a stationary game object with a body)

The answer is to use layers, commonly used for filtering out camera views, they can also be used to filter out physics collisions. In your project settings look for “Tags and Layers” pick an empty “user layer” and rename it to something like “missile”. Again in project settings look for the Physics tab, at the bottom of this tab there should be a collision matrix, looking something like those old logic puzzles with a row and column for each layer. Ensure that the tick box where missile and missile intersect is unchecked, finally set your bullet/missile prefabs layer to missile and you are ready to go.

You should now find that missiles simply don’t interact with each other, your fan of missiles looks pristine and they collide as they should, bullet hell can reign !

Leave a comment

Your email address will not be published. Required fields are marked *