In this lesson, you will learn how to boost your game’s performance by telling the physics engine exactly which objects are allowed to touch each other.
The Physics Cost of "Everything"
By default, every object with a Collider in your scene checks for potential collisions with every other Collider. If you have hundreds of objects—like debris, decorative props, or background elements—the CPU wastes precious cycles calculating collisions for objects that don't need to interact. This is like a security guard checking the ID of every single person in a building, even the ones who are supposed to be there, when you could just leave the interior doors open.
The Layer Collision Matrix
Unity provides a powerful tool called the Layer Collision Matrix to solve this. Located in Edit > Project Settings > Physics, this grid allows you to toggle collision detection on or off between specific layers.
For example, if you have a "Player" layer and a "DecorativeProps" layer, and you know your player will never interact with those props, you can uncheck the box where these two layers intersect in the matrix. Once disabled, the physics engine will completely skip collision checks between those two categories, saving performance instantly.
Practical Application
While the Matrix handles global rules, you can also optimize logic within your scripts to prevent unnecessary calculations. If you find yourself checking tags in OnCollisionEnter just to ignore them, you are already too late—the physics engine has already done the heavy lifting.
// Instead of this:
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.CompareTag("IgnoreMe")) return; // Physics already happened!
}
// Do this:
// 1. Assign "IgnoreMe" objects to a custom layer (e.g., "NonCollidable")
// 2. Go to Physics Settings and uncheck the "NonCollidable"
// layer in the Collision Matrix.
By configuring the Matrix, you prevent the physics engine from doing the "math" entirely, rather than just ignoring the result after the fact. This keeps your frame rate stable as your scenes grow in complexity.
Key Takeaway: Use the Layer Collision Matrix to disable collision calculations between layers that don't need to interact, reducing the CPU overhead of your physics simulation.