- Home /
How to edit PolygonCollider2D vertices IN CODE?
Hi,
So I understand that I can edit the polygon collider in the editor using shift, but what if I want to edit it during runtime via code? If editing them is not possible, surely I can at least see location of each vertex of the collider?
I am trying the below and getting the following error:
foreach (Vector2 vert in col.poly.paths[0])
error: Type `UnityEngine.Collider2D' does not contain a definition for `poly' and no extension method `poly' of type `UnityEngine.Collider2D' could be found (are you missing a using directive or an assembly reference?)
Where col is a PolygonCollider2D
Answer by dvdkong · Apr 21, 2014 at 09:45 AM
PolygonCollider2D polygonCollider = GetComponent();
polygonCollider.points = new [] { new Vector2(-Constants.cCellSize/2f,-Constants.cCellSize/2f), new Vector2(Constants.cCellSize/2f, - Constants.cCellSize/2f), new Vector2(Constants.cCellSize/2f,Constants.cCellSize/2f) };
polygonCollider.SetPath(0, new []
{
new Vector2(-Constants.cCellSize/2f,-Constants.cCellSize/2f), new Vector2(Constants.cCellSize/2f, - Constants.cCellSize/2f), new Vector2(Constants.cCellSize/2f,Constants.cCellSize/2f)
});
oh it is just some const value of grid cell size like this
public class Constants { public const float cCellSize = 32.0f; }
Your answer
Follow this Question
Related Questions
Why can I not directly set the points of PolygonCollider2D? 0 Answers
How to create a 3d polygon collider? 3 Answers
draw canvas/2D space Box Colliders on top of 3D objects that are located in world space? 0 Answers
Multiple Cars not working 1 Answer
Change polygon collider depending on the animation 0 Answers