Course contentsShow
← Unity Game Development
Lesson 166 of 8283. Physics and InteractionFree lesson

Visualizing Physics Contacts for Debugging

Utilize OnDrawGizmos to visualize collision contact points and trigger volumes in the editor to troubleshoot complex interaction logic.

In this lesson, you will learn how to use OnDrawGizmos to render invisible physics data directly into your Scene view, making it easy to spot exactly where collisions are occurring.

Seeing the Invisible

When debugging complex interactions, relying solely on logs can be frustrating because you can’t "see" exactly where Unity perceives a collision point or the boundaries of a trigger volume. The OnDrawGizmos method is a special Unity callback that runs even when your game is not playing, allowing you to draw lines, spheres, and labels that represent your physical logic in real-time.

Visualizing Collision Contacts

If you have a character that occasionally fails to trigger an interaction, you can use OnCollisionEnter to store the contact point and then draw it using Gizmos.

using UnityEngine;

public class PhysicsVisualizer : MonoBehaviour
{
    private Vector3 lastContactPoint;

    private void OnCollisionEnter(Collision collision)
    {
        // Store the first contact point from the collision data
        lastContactPoint = collision.contacts[0].point;
    }

    private void OnDrawGizmos()
    {
        // Draw a red sphere at the last known contact point
        Gizmos.color = Color.red;
        Gizmos.DrawSphere(lastContactPoint, 0.1f);
    }
}

Debugging Trigger Volumes

Triggers are invisible by default, which often leads to confusion if they are misaligned. You can visualize them by drawing the bounds of your collider inside OnDrawGizmos. Simply check for a Collider component on your GameObject and draw its wireframe. By changing the Gizmos.color based on a boolean (e.g., isTriggered), you create a visual indicator that flashes in the Scene view the moment a player enters the zone.

This approach transforms your debugging workflow from "guessing" why a script didn't fire to "seeing" the exact coordinate or volume that failed to register. Because OnDrawGizmos is limited to the editor, you don't need to worry about stripping this code before building your game; Unity handles the exclusion for you.

Key Takeaway: Use OnDrawGizmos to render collision points and trigger volumes in the Scene view to transform abstract physics data into actionable visual feedback.