Loading lessons...
Collision Matrix & Layers
Collision Matrix & Layers
As your game grows, you will want to restrict which objects can collide with each other (e.g. bullets should hit enemies and walls, but pass through other bullets and powerups).
GameObjects Layers
Every GameObject in Unity can be assigned to a Layer (configured at the top-right of the Inspector).
- Unity provides built-in layers: Default, TransparentFX, Ignore Raycast, Water, and UI.
- You can define custom layers (e.g. "Player", "Enemy", "PlayerBullet", "EnemyBullet").
The Collision Matrix
The Collision Matrix defines which layers are allowed to collide with each other physically.
To configure this matrix:
- Go to Edit -> Project Settings -> Physics.
- Scroll down to the Layer Collision Matrix grid.
- Check or uncheck the intersections to enable or disable collisions between specific layers.
For example, by unchecking the intersection of "PlayerBullet" and "PlayerBullet", bullets will pass through each other without causing physical collision events, reducing physics calculations.
TL;DR
- Assign GameObjects to Layers to categorize them.
- Access the Layer Collision Matrix under Edit -> Project Settings -> Physics.
- Toggle layer intersections to control which object classes collide.
- Disabling unnecessary collisions saves performance.