Search Unity

Is it possible to create a 2D Polygon Collider & Mesh that contains an internal enclosed gap?

Discussion in '2D' started by strikenowhere, Sep 9, 2021.

  1. strikenowhere

    strikenowhere

    Joined:
    May 6, 2021
    Posts:
    7
    Hello,

    As my title states, is it possible to create a PolygonCollider2D and Mesh such that there is a gap or hole that is completely within the outer boundary of the shape the collider/mesh is mimicking? A simple example would be a donut/torus shape, with a circle cut out of the geometry in the middle. The boundary points have to be calculated during script startup as well.
     
    Last edited: Sep 9, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Have you tried the obvious way of just wrapping the collider around an area in the middle that it excludes? It worked for me.
     
  3. strikenowhere

    strikenowhere

    Joined:
    May 6, 2021
    Posts:
    7
    Hi Kurt thank you for the reply but I'm not quite sure I follow. I have a rectangle around which I need to generate a set of colliders & meshes, excluding the area that the rectangle takes up along with the space taken up by each inner collider/mesh (please see the attached pic): Colliders.png

    I need each collider & mesh to only take up the space shaded by the yellow/blue/red colors, respectively but it seems to me right now that a PolygonCollider2D can't have any internal gaps or spaces. Is this not the case? Are you able to tell a PolygonCollider2D to ignore a region of space, thereby creating the necessary gaps?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    This is how I would do it: close up that gap in the upper left.
    Screen Shot 2021-09-09 at 7.24.09 AM.png
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Yes, use opposing windings for your paths. If the outer winding is clockwise, make the inner one anti-clockwise; that'll form a hole.
     
    Kurt-Dekker likes this.
  6. strikenowhere

    strikenowhere

    Joined:
    May 6, 2021
    Posts:
    7
    Thanks guys for the replies - I wound up creating an inner- & outer-bounds set of points as suggested to define the shape and the PolygonCollider2D correctly interpreted my points. From there I was able to generate a set of triangles using the built-in PolygonCollider2D.CreateMesh() function - instead of the old Triangulator code - to create the mesh data and now I have a functioning set of ring meshes & colliders.
     
    MelvMay and Kurt-Dekker like this.