Search Unity

2D Colliders on lined-up tiles making not-quite-flat surfaces

Discussion in '2D' started by seejayjames, Jan 21, 2020.

  1. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Hello! I have a set of ground tiles with box colliders on them, and they are evenly spaced and exactly the same Y position (duplicated and moved along the X axis only). The character will occasionally bump the colliders or get stuck when moving along what otherwise appears to be a flat, seamless collider that's made up of all the colliders in a perfect (?) line.

    I've tried a lot of variations but the only thing that works is to put one box collider (or an edge collider if I don't care about collisions underneath) on the entire piece of ground/platform...and would have to do that for every piece in the game, ugh. This is doable but not ideal, it would be best if the tile colliders could be lined up exactly (which they appear to be visually and numerically but collisions still happen, though inconsistently). Is this some sort of physics rounding error? Any workarounds? Thanks!
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    This comes up many times. You should know that physics systems don't consider colliders that are "aligned" to be a continuous surface because collisions take place between individual colliders.

    Only the EdgeCollider2D provides a continuous surface.

    There is the CompositeCollider2D which can merge multiple PolygonCollider2D and BoxCollider2D into a single collider and you can set it to use outline mode (continuous edges). There's plenty of info and tutorials on the composite so I would recommend looking at that.
     
    seejayjames likes this.
  3. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Thanks so much for the reply. I did look into the CompositeCollider2D but need to experiment with it more to get it working easily. Seems like the BoxCollider2D is more or less a continuous surface too, unless I'm missing something (with thorough testing there were no issues, at least). So I'll try to get a good workflow where I add an overall collider to each different-length segment that I use in the game. Creating a parent for the segments and adding the overall collider on the parent should be pretty quick.

    Thanks again!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    An individual primitive shape does form a continuous surface itself but two placed together don't act like one continuous surface; they are dealt with individually so you can get "ghost" collisions when moving across them.

    Note that a PolygonCollider2D can produce multiple primitive convex polygon shapes when your outline is concave and these separate primitive shapes are what are dealt with individually so a PolygonCollider2D might not form a continuous surface itself if it's comprised of multiple shapes.

    Box, Circle, Capsule, Edge colliders all form continuous surfaces because they use simple primitives. PolygonCollider2D and TilemapCollider2D don't.

    The CompositeCollider2D takes the geometry from colliders that support it and merges the geometry together automatically. If you use the Outline mode then you get nice contnuous surfaces. If you use Polygon mode then it acts like a PolygonCollider2D producing multiple convex polygon shapes so isn't continuous.
     
    batukan6699 and seejayjames like this.
  5. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Great to get some more details, I really appreciate it. That clears up some questions I had about the Polygon collider as well. I'll mess around with the Composite Collider and see if that's an efficient workflow for creating platforms and other parts of the game. If not, I can always add Box and Edge colliders manually, which really won't take that long.
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    There are lots of good YouTube tutorials on the CompositeCollider2D so maybe worth watching one or two to get a better idea of how to integrate it into your workflow.

    Whilst not a tutorial, this is a video I did when I created it several years back: