0

I got a polygon collider and i need a way to get its size. bounds.size is not an option, because it needs to be independent of the rotation of the object.

every comment is appreciated ^^

Bambous
  • 3
  • 1

2 Answers2

0

I don't know if there is an exact way of calculating the area of a collider in Unity but I think you can estimate it. You can give scale value as r-value (radius) of the circle and calculate it's an area of with pi*r^2 formula. It's like:

  • If you have a hexagon collider with a scale of 1.3 you can calculate pi*1.3^2

This might be a solution.

HTugsadK
  • 81
  • 5
  • in such case the question would be: Why is OP not simply using a `CircleCollider`? -> Probably because OP's use case is not that simple, therefore the question here ;) – derHugo Mar 02 '22 at 11:42
-1
 // Store these outside the method so it can reuse the Lists (free performance)
 private List<Vector2> points = new List<Vector2>();
 private List<Vector2> simplifiedPoints = new List<Vector2>();
 public void UpdatePolygonCollider2D(float tolerance = 0.05f)
 {
     polygonCollider2D.pathCount = sprite.GetPhysicsShapeCount();
     for(int i = 0; i < polygonCollider2D.pathCount; i++)
     {
         sprite.GetPhysicsShape(i, points);
         LineUtility.Simplify(points, tolerance, simplifiedPoints);
         polygonCollider2D.SetPath(i, simplifiedPoints);
     }
 }

This way you don't have to destroy / add a new PolygonCollider2D each time, plus the points List can be recycled for some free optimization. I hope it is useful for you.