Polygon Collider 2D can only make regular pentagons?

Hello! I’m just starting out with Unity, playing around with some 2D physics.

It seems to me that whenever I generate a mesh consisting of five irregular points, and add a “Polygon Collider 2D”, the collider only makes regular pentagons that don’t align with the mesh correctly. This results in bad collision physics.

Unity Manual says the collision points are “non-editable”, so I guess there is no way to set them correctly with code? I can move the points around in the editor but these gameobjects are generated with code so that’s not a solution.

Is there a way to make the Polygon Collider 2D understand the shape of the mesh better?

7502219--924335--pentagon.png

If you read the docs it seems to only care about sprites, not general geometry.

That said if you had a simple mesh you could probably make your own hull for it from your geometry.

You can also hand-change the points with the “Edit Collider” button.

Hmm. What’s the right way to do collisions for general 2D geometry then? If it isn’t Polygon Collider 2D? I don’t know what you mean by “making my own hull”.

As I said, I need to do this in code, not the editor. Because the objects are generated during gameplay and have random shapes.

That’s confusing. The Points field you see in the editor is (apparently) not editable.

BUT… you can get at .points from a PolygonCollder2D and manipulate the points yourself directly.

Here’s me doing it to create a chunk of 3D geometry, put a 2D collider on it and drop it:

https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/2d/makepolygoncollider2d/MakePolygonCollider2D.cs

No idea if they have to already be a convex hull or if it will figure that out for you.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

Ohh, I can edit the points after all! That makes a lot more sense.

The word “non-editable” led me astray. Because my script that tried to edit the points didn’t work, I assumed it’s because they’re “non-editable” as the manual says (actually it was just my coding error). I gave it another shot now and the points can indeed be edited just fine! Thanks a lot for clearing this up, much appreciated!

On a side note, I’m still not sure what the “non-editable” means there. I can edit the points in the editor just fine too.

1 Like