- Home /
Editing Polygon Collider 2D Mesh by Script
Hey guys, I am working on an isometric 2D terrain generation with which I want to be able to interact with the mouse later. I am creating all the GameObjects within a script and need the tiles to have a Polygon Collider 2D for the mouse interaction. I am far from satisfied with the mesh Unity creates for each tile and want to edit them manually. I do know that I could create a Prefab and edit the Polygon Collider there but I want the colliders to be as precise as possible so that there are no holes and no overlaps in the final terrain and the best way to do that is by placing them in a script with precise coordinates.
![image][1]
I have attached an image with three current phases I am encountering. The first kind(left) happens when I create the GameObject with a script and add the Polygon Collider 2D as a component. The second one(top right) is when I throw the sprite into the scene myself and add the Polygon Collider 2D in the Inspector. And the final one(bottom right) is the way I want it to be only the gras should be seen as a collider.
I have started to access the Polygon Collider 2D component in a script but can't think of a way check for current placement of the vertices of the collider mesh or how to edit them. [1]: /storage/temp/24857-polycollider2d.jpg
Answer by Leuthil · Dec 23, 2015 at 05:40 PM
I have found that just using SetPath of a PolygonCollider2D can sometimes behave strangely, especially if you are doing multiple paths. I found that setting pathCount first prevents this chance of strange behaviour.
polygonCollider2d.pathCount = 1;
// firstPath is an array of Vector2's defining all of the points in your collider
polygonCollider2d.SetPath(0, firstPath);
Or for more than one path:
polygonCollider2d.pathCount = 3;
polygonCollider2d.SetPath(0, firstPath);
polygonCollider2d.SetPath(1, secondPath);
polygonCollider2d.SetPath(2, thirdPath);
A small warning: at least in Unity 5.5, SetPath seems extraordinarily inefficient, spending nearly all it's time in "Physics2d.ColliderCleanup" as the number of points increases.
You can avoid this problem by disabling the PolygonCollider2D while setting the paths:
polygonCollider2d.enabled = false;
polygonCollider2d.pathCount = 3;
polygonCollider2d.SetPath(0, firstPath);
polygonCollider2d.SetPath(1, secondPath);
polygonCollider2d.SetPath(2, thirdPath);
polygonCollider2d.enabled = true;
Your answer
![](https://koobas.hobune.stream/wayback/20220613142127im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Polygon Collider 2D Not Precise Enough 0 Answers
2D Sprite, Animation Question 1 Answer
Why doesn't my hexagonal collision fit properly? 1 Answer
How to update EdgeCollider2D with sprite animation? 1 Answer
Tile Generation with collider2D's 1 Answer