Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Check if position is inside a composite collider 2D

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

  1. Darkkingdom


    Sep 2, 2014
    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


    Jan 9, 2019
    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.     }
    idan48 and Darkkingdom like this.
  3. MelvMay


    Unity Technologies

    May 24, 2013
    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.
    idan48, nicmarxp and Darkkingdom like this.
  4. Darkkingdom


    Sep 2, 2014
    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


    Feb 13, 2016
    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!