- Home /
Cycle through each vertex of a Polygon Collider 2D?
At the start of a level, I want to attach something to the closest vertex of all nearby colliders. So I use Physics2d.OverlapCircleAll to get all nearby colliders, then I use a foreach loop to iterate through the colliders and a nested foreach loop within to cycle through the verticies of each collider, checking the distance within the 2nd loop.
However I don't know how to cycle through the vertices that each collider2d consist of. If col is a polygonCollider2D, I thought it would either be col.poly.paths[0] or col.colliderInfo.verticies, but neither of those work. I realize that how to do this will vary depending on the collider, but right now I'm interested in the polygon collider.
You don't explain what is not working. Assu$$anonymous$$g you have a reference to a PolygonCollider2D, then you are loping for 'points'. Points will not be available in the base class of Collider2D, plus the points will be in local space. To do your check, you'll have to convert.
Answer by KEELAN · Aug 23, 2014 at 12:06 PM
Hey there, this should work for you.
void polyVertCycle(PolygonCollider2D col)
{
Vector2[] points = col.points;
for(int i = 0;i<points.Length ;i++)
{
points[i] = //do something with vector2
}
}
EDIT: only saw the date after sending
Your answer
Follow this Question
Related Questions
Unity 4.3, generate 2d mesh 2 Answers
gameObject with 2 colliders with different layerMasks ? 1 Answer
OnTriggerEnter2D Doesn't fire after switching to IsTrigger on 2 Answers
Trouble with composite and polygon collider in player 0 Answers
How to get the closest vertex of the Polygon Collider 2D in code? 1 Answer