Debug.DrawBox function is direly needed.

Greetings.

It would be very nice, if Debug would have analog of DrawRay, but for BoxCast. Physics2D.BoxCast is one of most frequently used ways to make detection of land, ledges, walls and steps, but since there is no way to visualise work of BoxCast using fail-safe and reliable Unity functions, it is sometimes difficult to understand, if calculations of start position and cast length were correct, where exactly casted collider and it’s way would be. Thus Debug.DrawBox, which would draw rectangle containing box and it’s cast way, would come in handy.
I surely hope I chose correct category and tags to post this request.

Sincerely, realGuybrush.

1 Like

You can use

and

or

Or you could just draw the 12 edges of your box using Debug.DrawLine.

Here’s one approach

        public void DrawBox(Vector3 pos, Quaternion rot, Vector3 scale, Color c)
        {
            // create matrix
            Matrix4x4 m = new Matrix4x4();
            m.SetTRS(pos, rot, scale);

            var point1 = m.MultiplyPoint(new Vector3(-0.5f, -0.5f, 0.5f));
            var point2 = m.MultiplyPoint(new Vector3(0.5f, -0.5f, 0.5f));
            var point3 = m.MultiplyPoint(new Vector3(0.5f, -0.5f, -0.5f));
            var point4 = m.MultiplyPoint(new Vector3(-0.5f, -0.5f, -0.5f));

            var point5 = m.MultiplyPoint(new Vector3(-0.5f, 0.5f, 0.5f));
            var point6 = m.MultiplyPoint(new Vector3(0.5f, 0.5f, 0.5f));
            var point7 = m.MultiplyPoint(new Vector3(0.5f, 0.5f, -0.5f));
            var point8 = m.MultiplyPoint(new Vector3(-0.5f, 0.5f, -0.5f));

            Debug.DrawLine(point1, point2, c);
            Debug.DrawLine(point2, point3, c);
            Debug.DrawLine(point3, point4, c);
            Debug.DrawLine(point4, point1, c);

            Debug.DrawLine(point5, point6, c);
            Debug.DrawLine(point6, point7, c);
            Debug.DrawLine(point7, point8, c);
            Debug.DrawLine(point8, point5, c);

            Debug.DrawLine(point1, point5, c);
            Debug.DrawLine(point2, point6, c);
            Debug.DrawLine(point3, point7, c);
            Debug.DrawLine(point4, point8, c);

            // optional axis display
            Debug.DrawRay(m.GetPosition(), m.GetForward(), Color.magenta);
            Debug.DrawRay(m.GetPosition(), m.GetUp(), Color.yellow);
            Debug.DrawRay(m.GetPosition(), m.GetRight(), Color.red);
        }
8 Likes

An easy hack is to make your regular gameObject “debugging” box. Instead of Debug.DrawBox use myDebugBox.position=. Give it a partly-transparent texture so you can see better what’s it’s on. That’s solved many of my debugging issues.

It’s often good enough to leave it in the last place it was put, but if that’s too confusing then hide the box at the start of each frame and enable it in the debug line.

2 Likes