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

2D Collider Custom Shape

Discussion in '2D' started by SimteractiveDev, Sep 2, 2014.

  1. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Hi,

    I'm creating an isometric 2D game and I want to create a custom collider for my sprites so the collider only covers the base of the sprites, for example the collider would cover the green highlighted bit below:



    The PolygonCollider covers the entire image, and while it is possible to edit and remove the vertices, it would be pretty time consuming to do that for all sprites in the game. The EdgeCollider seems ideal, however you cannot join 2 edges together or add a cap, meaning no collisions are detected unless you hit the edge (the user will be able to tap the objects).

    Anybody have any ideas how I could create a collider to cover the above highlighted area?
     
  2. Quaker_SDR

    Quaker_SDR

    Joined:
    Jun 21, 2013
    Posts:
    39
    You can use the 2d polygon collider to get all desired shapes that you need.
     
    OCTOGOON likes this.
  3. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    The problem with the 2d polygon collider is that it automatically generates about 200 vertices for each sprite, so it's pretty time-consuming to go through and delete them all. Is there any plugins/extensions that makes this process easier?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    I have small helper script in asset store for that,
    see info/examples here:
    http://unitycoder.com/blog/2014/03/30/asset-store-polygoncollider2d-optimizer/
     
  5. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    It just filters the extra vertices automatically (and removes holes, if needed),
    then you can edit that polygon2D collider normally.

    If you only want to create that square/rectangle collider (4 vertices) as in your image,
    wouldnt it be easy just to copy the same polygoncollider to other objects (if they are same size?)
     
    SimteractiveDev likes this.
  7. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Ah, I was trying to copy the collider component by dragging it onto the new object which didn't work, so assumed I could copy collider components. Never saw the option in the drop-down menu. Awesome, thanks very much, you are a life saver!
     
  8. GreedyDev

    GreedyDev

    Joined:
    Jun 12, 2020
    Posts:
    1
    hey, I'm in 2020 and I just saw your message and, I also need some help and I couldn't get this to work.
     
  9. Jabberwoky

    Jabberwoky

    Joined:
    Apr 6, 2020
    Posts:
    5
    you can set the default Polygon Collider Shape in the SpriteEditor in Custom Physics Shape
     
    reinfeldx likes this.