# visualize a 2d boxcast with this simple wrapper method

Discussion in 'Scripting' started by Ruskul, Jul 11, 2016.

1. ### Ruskul

Joined:
Aug 7, 2014
Posts:
16
static public RaycastHit2D BoxCast( Vector2 origen, Vector2 size, float angle, Vector2 direction, float distance, int mask ) {
RaycastHit2D hit = Physics2D.BoxCast(origen, size, angle, direction, distance, mask);

//Setting up the points to draw the cast
Vector2 p1, p2, p3, p4, p5, p6, p7, p8;
float w = size.x * 0.5f;
float h = size.y * 0.5f;
p1 = new Vector2(-w, h);
p2 = new Vector2(w, h);
p3 = new Vector2(w, -h);
p4 = new Vector2(-w, -h);

Quaternion q = Quaternion.AngleAxis(angle, new Vector3(0, 0, 1));
p1 = q * p1;
p2 = q * p2;
p3 = q * p3;
p4 = q * p4;

p1 += origen;
p2 += origen;
p3 += origen;
p4 += origen;

Vector2 realDistance = direction.normalized * distance;
p5 = p1 + realDistance;
p6 = p2 + realDistance;
p7 = p3 + realDistance;
p8 = p4 + realDistance;

//Drawing the cast
Color castColor = hit ? Color.red : Color.green;
Debug.DrawLine(p1, p2, castColor);
Debug.DrawLine(p2, p3, castColor);
Debug.DrawLine(p3, p4, castColor);
Debug.DrawLine(p4, p1, castColor);

Debug.DrawLine(p5, p6, castColor);
Debug.DrawLine(p6, p7, castColor);
Debug.DrawLine(p7, p8, castColor);
Debug.DrawLine(p8, p5, castColor);

Debug.DrawLine(p1, p5, Color.grey);
Debug.DrawLine(p2, p6, Color.grey);
Debug.DrawLine(p3, p7, Color.grey);
Debug.DrawLine(p4, p8, Color.grey);
if(hit) {
Debug.DrawLine(hit.point, hit.point + hit.normal.normalized * 0.2f, Color.yellow);
}

return hit;

2. ### gumboots

Joined:
May 24, 2011
Posts:
268
Just wanted to say thanks for this, it works really well and helped me get my mind around what I was trying to do!

3. ### xVergilx

Joined:
Dec 22, 2014
Posts:
1,658
You can always simplify thing by using Gizmos.DrawWireBox() inside OnDrawGizmos() / OnDrawGizmosSelected().