woensdag 31 oktober 2012

Managing collisions

Dear readers,

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
So instead of putting all game objects on the same layer which would cause collisions to be checked against every game object (that is also a physic object, i.e. a game object with a rigidbody and a collider component), I create multiple layers and set which layers should collide and which shouldn't. Of course this will in most cases affect the gameplay. Although it would be awesome if everything would just collide with everything, it just isn't worth it when your game starts running at 5 frames per second.

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.
-- Stijn

1 opmerking: