Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

visualize a 2d boxcast with this simple wrapper method

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

  1. Ruskul

    Ruskul

    Joined:
    Aug 7, 2014
    Posts:
    72
    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;
     
    The_Jeff, DuckSmacker and MaykeBr like this.
  2. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    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

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You can always simplify thing by using Gizmos.DrawWireBox() inside OnDrawGizmos() / OnDrawGizmosSelected().
     
  4. SolidAlloy

    SolidAlloy

    Joined:
    Oct 21, 2019
    Posts:
    58
    This will simplify the code in terms of the number of lines, but you will have to separate casting the box and visualizing it into different methods, which decreases the code readability.
     
  5. SolidAlloy

    SolidAlloy

    Joined:
    Oct 21, 2019
    Posts:
    58
    Thanks for the script! There are a lot of suggestions on how to do it for Physics 3D on the Internet, but this is the only one I saw for Physics2D. I improved it a little by adding a method to cast a box and draw in a single method, if you don't mind: link to the gist
     
    pistoleta likes this.
  6. Ruskul

    Ruskul

    Joined:
    Aug 7, 2014
    Posts:
    72
    I'm glad you found it useful. I forgot all about even posting this, I don't mind at all
     
    SolidAlloy likes this.
  7. sollivan

    sollivan

    Joined:
    Apr 12, 2020
    Posts:
    1
    This script save my life! I always think about why Unity doesn't have a feature like this...