Today I'd like to talk about collisions between objects in physics and how to manage them. Since we finished 'Downtown Drift' (which is now online here and you should definitely play: http://www.a10.com/racing-games/downtown-drift) we've been working on our next game involving lots of cars, explosions and wrecking. During the development of our next game performance became an issue. This was mainly due to there being too many physic objects checking for collisions with other physic objects. In 'Downtown Drift' this wasn't a problem as there really wasn't a lot of collision going on, just collisions between a couple of cars and the street mainly.
The way I solved this was by creating more layers. In Unity each game object is on a certain layer. Layers are basically a way to group your game objects. Certain functionality can be applied on one or more layers. For instance cameras can be made to only render some layers and not others. Collisions can be controlled with layers as well. Unity allows us to decide which layers should have collision with other layers using the layer collision matrix shown below.
|The 'Layer Collision Matrix' in Unity|
|The destructible layer with which the player can collide, but that doesn't collide with traffic.|
|The traffic layer with which the player can collide, but that doesn't collide with destructibles.|