Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Whats the best way to debug Physics2D.OverlapArea?

Discussion in 'Physics' started by enhawk, Dec 2, 2015.

  1. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    832
    I have an object that is moving around and I need to visually see where my Physics2D.OverlapArea is to debug it.

    I'm trying to use the following code but it makes the gizmos change size when the object moves.

    Code (csharp):
    1.  
    2.     void OnDrawGizmosSelected() {
    3.         Gizmos.color = Color.blue;
    4.         Gizmos.DrawCube(transform.position, new Vector3(bottomSensor[1].x, bottomSensor[1].y, 0));
    5.         Gizmos.color = Color.red;
    6.         Gizmos.DrawCube(transform.position, new Vector3(bottomSensor[3].x, bottomSensor[3].y, 0));
    7.     }
    8.  
    here's a sample of the code I'm using to create the overlaps, it is creating a sample overlap area under the object on the left and right:

    Code (csharp):
    1.  
    2. void LateUpdate() {
    3.      
    4.         // Bottom Left.
    5.         bottomSensor[0] = new Vector2(
    6.             transform.position.x - 1f,
    7.             transform.position.y
    8.         );
    9.         bottomSensor[1] =    new Vector2(
    10.             transform.position.x,
    11.             transform.position.y - 0.1f
    12.         );
    13.  
    14.         // Bottom Right.
    15.         bottomSensor[2] = new Vector2(
    16.             transform.position.x,
    17.             transform.position.y
    18.         );
    19.         bottomSensor[3] =    new Vector2(
    20.             transform.position.x + 1f,
    21.             transform.position.y - 0.1f
    22.         );
    23.  
    24.         groundObjectsLeft = Physics2D.OverlapAreaAll(bottomSensor[0], bottomSensor[1]);
    25.         groundObjectsRight = Physics2D.OverlapAreaAll(bottomSensor[2], bottomSensor[3]);
    26. }
    The problem I am having is that the Gizmo requires a center axis, and a size, whereas Physics2D.OverlapArea requires a topleft and bottom right set of two Vector2's. How do I translate this to Gizmos.DrawCube?
     
  2. 10HPLeft

    10HPLeft

    Joined:
    Mar 23, 2014
    Posts:
    3
    My suggestion is to create a mesh based on the overlapping area. Something like this code snippet:

    Vector3 a, b; // 2d collider path intersections.
    Mesh overlapAreaMesh = new Mesh();
    overlapAreaMesh.vertices = new Vector3[] {a, new Vector3(a.x, b.y, 0), new Vector3(b.x, a.y, 0), b};
    overlapAreaMesh.triangles = new int[] {0, 1, 2, 3, 2, 1};


    Then attaching that new mesh to a renderer with a sweet material.