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 ^^
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 ^^
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:
This might be a solution.
// 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.