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

Physics2D.OverlapBox not working on Composite Colliders

Discussion in '2D' started by TheLastBert, Jan 29, 2019.

  1. TheLastBert

    TheLastBert

    Joined:
    Mar 10, 2018
    Posts:
    3
    I'm using Physics2D.OverlapBox for my ground detection which works nicely, but once I have a tilemap my players starts catching on the edges between tiles.
    As soon as I add a CompositeCollider 2D and select Used by Composite on the Tilemap Collider 2D, the OverlapBox method no longer returns a collider when the box overlaps with the composite tilemap collider.
    I'm not using any layer mask, my code is simply:
    Code (CSharp):
    1. Collider2D collisionAtFeet = Physics2D.OverlapBox(new Vector2(boxX, boxY), new Vector2(halfColliderWidth, 0.2f), 0.0f);
    I can use Debug.DrawLine to confirm the box is in the right place.

    This looks like a bug in Unity to me, I'm hoping someone can tell me otherwise or provide a workaround.

    Thanks!
     
  2. TheLastBert

    TheLastBert

    Joined:
    Mar 10, 2018
    Posts:
    3
    Looking at the documentation, this is solved by selecting Polygons as the Geometry type, rather than Outlines, which is the default and essentially produces Edge colliders. This explains the behaviour I was seeing.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    OverlapBox will still detect outlines, they are effectively the same as the EdgeCollider2D. Of course they'll only detect the actual edges, there's no internal area for edges as they are effectively open shapes.
     
    mscardinal likes this.
  4. Zephni

    Zephni

    Joined:
    Apr 23, 2015
    Posts:
    100
    TLDR: By switching the Geometry Type to polygons, now Physics2D.OverlapPoint returns what you'd expect

    upload_2022-8-28_12-57-40.png
     
    Last edited: Aug 29, 2022
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    Yes, edges don't have an "inside", they are just edges even if they are positioned around a specific area. In short, edges are open shapes whereas polygons, circles, capsules (etc) are closed shapes with an inside.
     
    Zephni likes this.