In a 2D isometric setup, I have a sprite that defines the walkable areas of a level exactly, like this:

When I attach a Polygon Collider 2D to it, I get a collider that looks like this:
Clearly not usable for the actual collision, it’s oversimplified. Is there any way to generate an exact collider outline from a sprite? I’ve tried code like this: Automatically Create Polygon Collider 2D From 2D Mesh in Unity - H3XED but it’s just automating the same physics shape that appears here.
1 Like
I dont know why everyone in this answer is linking external plugins, Unity 2D has this feature already. Open up the Sprite editor and select Custom Physics Shape, then move the tolerance up and hit generate. 0 is vague but very non-intensive collision (What it defaults to), while 1 is extremely accurate to sprite shape but requires a lot of polygons for the collision mesh. Then just apply a polygon Collider to the sprite (May need to remove and re-add it if there is one already)

Hey, I’ve had the same issue in the past (for creating island collisions) and the way I solved it was to use the Advanced Polygon Collider (Advanced Polygon Collider | Physics | Unity Asset Store) asset. You pretty much can just drop the AdvancedPolygonCollider.cs script onto the game object that has the sprite and it will generate a PolygonCollider2D based on the outlines of the sprite. Also has customisable options like alpha tolerance and detecting islands and simplifying the collider to use less vertices.
Hope this helps
Hi! I’ve often seen unity do this sort of thing before. Consider taking a look at the “sprite assist” extension ( GitHub - sr4dev/Unity-SpriteAssist: Unity Sprite Mesh Extension ), it aims to pack the sprite triangles more efficiently during import. It seems to be exactly what you need in this situation 