Ben Traje
← Back to unity

Unity Physics Guide: 3D vs. 2D Colliders and the Z-Axis Trap

02 Sep 26 (1d ago)

When building a game in Unity, you will inevitably have to choose between 3D physics (BoxCollider) and 2D physics (BoxCollider2D). You can actually use both within the same project, but there is a massive catch: they do not talk to each other.

In Unity, the 3D physics engine (PhysX) and the 2D physics engine (Box2D) run on entirely separate tracks. A BoxCollider (3D) will never register a collision or a trigger event with a BoxCollider2D.

Here is the breakdown of how to choose the right physics system for your project.

The "Compatibility" Rule

You cannot mix and match components from different physics engines on the same GameObject. If you try to attach a 3D Rigidbody to a 2D Collider, Unity will not calculate the physics correctly.

Visual ObjectUse this Collider...And this Rigidbody...
3D Mesh / CubeBoxCollider, MeshColliderRigidbody
2D SpriteBoxCollider2D, CircleCollider2DRigidbody2D

Which one should you choose?

1. Use 2D Colliders if:

  • You want pixel-perfect precision: 2D colliders wrap around sprites much more cleanly.
  • You are building a traditional 2D game: Top-down RPGs, side-scrolling platformers, or UI-heavy physics games.
  • Performance is a priority: 2D physics (Box2D) only calculates math on the X and Y axes. Because it completely drops the complex math required for a third dimension, it is significantly "cheaper" on the CPU than 3D physics.
  • You need 2D-specific features: Unity includes specialized 2D components like the Platformer Effector 2D (for classic one-way platforms) or the Buoyancy Effector 2D.

2. Use 3D Colliders if:

  • You are making a 2.5D game: You are using 3D models but restricting gameplay to a 2D plane (like Super Smash Bros).
  • You need depth interactions: You want objects to pass "behind" or "in front" of each other in Z-space and physically collide based on depth.
  • You want 3D physics forces: You need to use built-in 3D physics calculations like AddExplosionForce.

Quick Tip: Unity does not actually care if your graphics are 2D or 3D. If you are using 2D Sprites but want 3D physics, you can just slap a 3D BoxCollider and a 3D Rigidbody on a Sprite. Unity will simply treat the flat sprite like a very thin, invisible box!

⚠️ The "Z-Axis" Trap (Fact Check!)

If you decide to use 2D Colliders in a 3D space, you need to understand how the engine handles depth. Many developers assume that if their Sprite is at Z = 10 (background) and the Ground is at Z = 0 (foreground), they will not collide.

This is entirely false.

Unity's 2D physics engine (Box2D) completely ignores the Z-axis. It evaluates everything purely on a flat X/Y grid. Even if two objects are 1,000 units apart on the Z-axis, they will still collide if their X and Y coordinates overlap.

If you want to prevent background 2D objects from colliding with foreground 2D objects, you cannot rely on Z-position. Instead, you must assign them to different Physics Layers and disable interactions between those layers in your Layer Collision Matrix (Edit > Project Settings > Physics 2D).

Bonus: How to Constrain a 3D Rigidbody to 2D Space

If you decide to use 3D physics for a 2.5D game, you need to stop your objects from falling over or drifting on the Z-axis. You can manually lock these constraints in the Inspector, but here is a quick C# script to enforce it dynamically via code:

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class ConstrainTo2D : MonoBehaviour
{
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        
        // Lock the Z position so the object doesn't drift forward/backward
        // Lock the X and Y rotation so the object doesn't tip over
        rb.constraints = RigidbodyConstraints.FreezePositionZ | 
                         RigidbodyConstraints.FreezeRotationX | 
                         RigidbodyConstraints.FreezeRotationY;
    }
}```

Attach this to any 3D object, and it will behave exactly like a 2D physics object while still utilizing the 3D PhysX engine!