Search Unity

get vertices of any collider2D

Discussion in '2D' started by Dahlvash, Aug 21, 2017.

  1. Dahlvash

    Dahlvash

    Joined:
    Feb 9, 2014
    Posts:
    54
    Hi.
    I've been trying to get the vertices of any Collider2D collider to use for a mesh generator (fill in gaps essentially).
    I found that PolygonCollider2D has a public points array, but no other 2D collider or composite collider.
    Am I missing something here or is there some other way to work it out?
    Thanks!
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Well box collider points can be found using bounds, circle collider you can use math to create points around that circle's perimeter using center and radius. A capsule is just two half-circles and a box connecting them, so same as previous. Composite colliders are only made up of Polygon colliders or Box colliders.

    So while there's no easy way to just get the points, it shouldn't be that hard to look up how to calculate the points based on the inputs on those components.
     
    Gamba04 likes this.
  3. Dahlvash

    Dahlvash

    Joined:
    Feb 9, 2014
    Posts:
    54
    Thanks for your reply!
    I was hoping to not have to check for every colliders type around the object and run different logic for each but its looking like I'll have to.
    I could replace all colliders with polygon colliders which would somewhat get around the issue but composite colliders (even set to polygon) still dont have a points array; but I'll make do :)
    Thanks again!
     
    takanaryota and LiterallyJeff like this.
  4. The_Pied_Shadow

    The_Pied_Shadow

    Joined:
    Jul 25, 2018
    Posts:
    14
    This is a 5 year old thread at this point but it was one of the first items that came up in a google search.

    I wanted to share a possible solution that should work for any Collider2D. I don't know if this was an option back in 2017.

    the Collider2D object has a CreateMesh() method which....creates a mesh from the collider. Now op said they were trying to create a mesh in the first place in which case, problem solved. But if you are looking for the points in the mesh then mesh has a vertices field which is what you're looking for.

    Hope this helps someone.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    If you want an arbitrary mesh that covers the area then yes but note that those vertices are not related to anything the physics system uses.

    In 2021.2 onwards you can get the actual shapes from any Collider2D using Collider2D.GetShapes. This gives you all the shape details including vertices, radius etc.