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
  4. Dismiss Notice

Question How to detecting overlapping polygon2D colliders?

Discussion in 'Scripting' started by xpire1, Jun 5, 2021.

  1. xpire1

    xpire1

    Joined:
    Oct 19, 2020
    Posts:
    7
    I'm trying to program an isometric building placement script. Each building has a PolygonCollider2D component on it with a trigger. When placing a new building, I'm trying to check if the PolygonCollider2D of the placed building overlaps with anything else (to check if placement is valid). My code is as follows:

    Adjust the points of the new placed building collider based on the mouse position
    Code (CSharp):
    1. Vector2[] points = polygonCollider2D.points;
    2. points.SetValue(polygonCollider2D.points[0] + (Vector2)mousePosition, 0);
    3. points.SetValue(polygonCollider2D.points[1] + (Vector2)mousePosition, 1);
    4. points.SetValue(polygonCollider2D.points[2] + (Vector2)mousePosition, 2);
    5. points.SetValue(polygonCollider2D.points[3] + (Vector2)mousePosition, 3);
    6. polygonCollider2D.points = points;
    Set up contact filter:

    Code (CSharp):
    1. ContactFilter2D contactFilter2D = new ContactFilter2D();
    2. contactFilter2D.useTriggers = true;
    3. contactFilter2D.SetLayerMask(polygonCollider2D.gameObject.layer);
    Check for collisions

    Code (CSharp):
    1. List<Collider2D> list = new List<Collider2D>();
    2. Debug.Log(polygonCollider2D.OverlapCollider(contactFilter2D, list));
    However if there is already a building there, it still does not register an overlap. What am I missing / doing wrong ?

    Thanks so much for the help!
     
  2. xpire1

    xpire1

    Joined:
    Oct 19, 2020
    Posts:
    7
    Hello
     
  3. xpire1

    xpire1

    Joined:
    Oct 19, 2020
    Posts:
    7
    anyone? please help
     
  4. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Stop being so rushy, someone who knows the answer will come and help, gotta be patient my guy