In this lesson, you will learn how to use built-in Unity tools to visualize hidden physics calculations, helping you identify why your collisions or character movements aren't behaving as expected.
Seeing the Invisible
When a character gets stuck on a slope or fails to trigger a collision, the problem is often happening in "invisible" space. Unity provides two powerful ways to see these calculations: the Physics Debugger and the Collision Matrix.
The Physics Debugger (found under Window > Analysis > Physics Debugger) is your primary lens into the simulation. It allows you to toggle the visibility of colliders, which is vital for spotting "ghost" colliders or objects that aren't positioned where the renderer makes them appear. Most importantly, it displays color-coded collision contact points. If your character’s Rigidbody is bumping into an invisible wall, the Debugger will draw small lines representing the contact normals, revealing exactly which collider is obstructing your movement.
Managing Layers with the Collision Matrix
Sometimes, your character isn't "colliding" because the physics engine is explicitly told not to. If you are dealing with complex interactions—like a character pushing a box but ignoring certain trigger volumes—you should check your Collision Matrix.
Located in Project Settings > Physics, the Collision Matrix acts as a master switch for layer interactions. If two layers are unchecked, they physically pass through each other. If you suspect an interaction failure, always verify that the layers assigned to your Player and the target environment are both checked.
Debugging with Code
Beyond editor tools, you can use Debug.DrawRay inside your FixedUpdate to visualize your ground-detection rays or push forces in the Scene view:
void FixedUpdate() {
// Visualizing a ground detection ray
Debug.DrawRay(transform.position, Vector3.down * rayDistance, Color.red);
if (Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, rayDistance)) {
// Visualizing the surface normal to debug slope detection
Debug.DrawLine(hit.point, hit.point + hit.normal, Color.green);
}
}
By visualizing the Raycast vectors, you can see if your detection logic is hitting the correct surface or missing it entirely due to a misaligned transform.
Key Takeaway: Use the Physics Debugger to verify collision contacts and the Collision Matrix to ensure physics layers are configured to interact as intended.