- Home /
How to generate exact match polygon collider from a sprite
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: https://www.h3xed.com/programming/automatically-create-polygon-collider-2d-from-2d-mesh-in-unity but it's just automating the same physics shape that appears here.
I believe the composite collider 2d isn't convex, so it can wrap around the mesh better.
Answer by AaronBacon · Jun 03 at 06:17 AM
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)
Wow, that's exactly what I was missing. Collisions are perfect now, thanks!
Answer by TheTrueDuck · Jun 02 at 10:03 AM
Hi! I've often seen unity do this sort of thing before. Consider taking a look at the "sprite assist" extension ( https://github.com/sr4dev/Unity-SpriteAssist ), it aims to pack the sprite triangles more efficiently during import. It seems to be exactly what you need in this situation :)
@TheTrueDuck awesome! So, that helped me generate a mesh that fits the shape better. But now how can I use it as a collider? If I add a Polygon Collider 2D onto the same component, it uses the original shape, not the improved one. If I add a Mesh Collider I can use the generated mesh, but I'm in a 2D isometric setup, thus Mesh Collider is incompatible with it.
Answer by ntbinquiries · Jun 02 at 06:08 PM
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 (https://assetstore.unity.com/packages/tools/physics/advanced-polygon-collider-52265#description) 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!
Dang, looks like exactly what I need but crashes on my image with:
Error creating collider: System.ArgumentException: Texture2D.GetPixels: texture data is either not readable, corrupted or does not exist. (Texture 'bedroom-collision') at (wrapper managed-to-native) UnityEngine.Texture2D.GetPixels(UnityEngine.Texture2D,int,int,int,int,int)
Have you tried checking the Read/Write enabled checkbox in the importer settings of the image? Judging by your error, the tool wants to access the individual pixels of the image but doesn't have permission to do so.
For the script to read the image data, the image has to have the "Read/Write" import settings under Advanced enabled
Your answer
Follow this Question
Related Questions
" " rect lies (partially) outside of texture. Will not generate Sprite for this Slice. 0 Answers
Why is the Enemy's Sprite Flipping When it Sees the Player?? 0 Answers
Grainy, Blurry, Washed Out Sprites 1 Answer
Colliders 2d apparently touch each other even if they should not. 2 Answers
Crop/Mask a Sprite on runtime 0 Answers