Search Unity

Debug.DrawBox function is direly needed.

Discussion in 'Scripting' started by realguybrush, Jan 13, 2021.

  1. realguybrush

    realguybrush

    Joined:
    May 7, 2018
    Posts:
    1
    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.
     
    pistoleta likes this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
  3. backwheelbates

    backwheelbates

    Joined:
    Jan 14, 2014
    Posts:
    232
    Here's one approach

    Code (CSharp):
    1.         public void DrawBox(Vector3 pos, Quaternion rot, Vector3 scale, Color c)
    2.         {
    3.             // create matrix
    4.             Matrix4x4 m = new Matrix4x4();
    5.             m.SetTRS(pos, rot, scale);
    6.  
    7.             var point1 = m.MultiplyPoint(new Vector3(-0.5f, -0.5f, 0.5f));
    8.             var point2 = m.MultiplyPoint(new Vector3(0.5f, -0.5f, 0.5f));
    9.             var point3 = m.MultiplyPoint(new Vector3(0.5f, -0.5f, -0.5f));
    10.             var point4 = m.MultiplyPoint(new Vector3(-0.5f, -0.5f, -0.5f));
    11.  
    12.             var point5 = m.MultiplyPoint(new Vector3(-0.5f, 0.5f, 0.5f));
    13.             var point6 = m.MultiplyPoint(new Vector3(0.5f, 0.5f, 0.5f));
    14.             var point7 = m.MultiplyPoint(new Vector3(0.5f, 0.5f, -0.5f));
    15.             var point8 = m.MultiplyPoint(new Vector3(-0.5f, 0.5f, -0.5f));
    16.  
    17.             Debug.DrawLine(point1, point2, c);
    18.             Debug.DrawLine(point2, point3, c);
    19.             Debug.DrawLine(point3, point4, c);
    20.             Debug.DrawLine(point4, point1, c);
    21.  
    22.             Debug.DrawLine(point5, point6, c);
    23.             Debug.DrawLine(point6, point7, c);
    24.             Debug.DrawLine(point7, point8, c);
    25.             Debug.DrawLine(point8, point5, c);
    26.  
    27.             Debug.DrawLine(point1, point5, c);
    28.             Debug.DrawLine(point2, point6, c);
    29.             Debug.DrawLine(point3, point7, c);
    30.             Debug.DrawLine(point4, point8, c);
    31.  
    32.             // optional axis display
    33.             Debug.DrawRay(m.GetPosition(), m.GetForward(), Color.magenta);
    34.             Debug.DrawRay(m.GetPosition(), m.GetUp(), Color.yellow);
    35.             Debug.DrawRay(m.GetPosition(), m.GetRight(), Color.red);
    36.         }
     
    niuage, MUGIK, zhaozony and 3 others like this.
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    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.
     
    AndreiTache and Kurt-Dekker like this.