Search Unity

Check if position is inside a composite collider 2D

Discussion in '2D' started by Darkkingdom, Mar 21, 2019.

  1. Darkkingdom

    Darkkingdom

    Joined:
    Sep 2, 2014
    Posts:
    81
    Hey guys,

    I'm currently working on a platformer with the new tiletool and I'm using composite collider 2D for my walkable ground.
    But now I have a spawn system for items which can appear at random position in a circle around the player.
    And sometimes these items spawn inside the ground / composite collider.
    I'd like to add a verification if the position isn't inside the collider.
    So I was wondering if there is anyway to check if a position is inside a composite collider 2D.
    I coudn't find anything on this question.


    Thank you very much :)
     
  2. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    I'm using a PolygonCollider2D for walkable ground, and had to solve this same problem. Extent is a Rect, which enforces the bounds of the game board. If your board doesn't have bounds, you can remove that check. instance.m_myLayer is a layer which is unique to the walkable ground.

    Code (CSharp):
    1.     public static bool OnBoard(Vector2 pos) {
    2.         if (Extent.Contains(pos)) {
    3.             Collider2D[] hits = Physics2D.OverlapCircleAll(pos, 0);
    4.             foreach (Collider2D hit in hits) {
    5.                 if (hit.gameObject.layer == instance.m_myLayer) {
    6.                     return false;
    7.                 }
    8.             }
    9.             return true;
    10.         }
    11.         return false;
    12.     }
    13.  
     
    idan48 and Darkkingdom like this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    The CompositeCollider2D is just like any other collider so you can use all queries on it so OverlapPoint, OverlapCircle, OverlapCollider etc. Of course, polygon mode produces closed polygon regions (they have an inside) whereas outline mode produces open edges (they do not have an inside). Outline mode means you can only detect the edges themselves in the same way as an EdgeCollider2D.

    So in short, in polygon mode then just use standard queries. For outline mode, you cannot detect the "inside" because there isn't any (the inside is the same as the outside) and you're not overlapped unless you're over the line itself.
     
  4. Darkkingdom

    Darkkingdom

    Joined:
    Sep 2, 2014
    Posts:
    81
    Many thank for both of your answers!
    The outline mode was exactly my problem since my algorithm did something simlar like zioths.
    So I just switched to polygon mode and now it works.
    Thank you very much :)
     
    Last edited: Mar 22, 2019
    MelvMay likes this.
  5. banjolasse

    banjolasse

    Joined:
    Feb 13, 2016
    Posts:
    11
    Wow, thank you so much for this answer. I just assumed Outline/Polygon was just how it was shown in the editor (pretty stupid in retrospect...) and our character sometimes clipped thru the world if you hit an edge just right. Hopefully that elusive bug is now fixed. Cheers!