public static float ComputeInertia(Rigidbody2D body) { //CALCULATE INERTIA, TAKING SCALING AND ALL THE CHILD COLLIDERS INTO ACCOUNT float hypothetic_mass = 0f; float hypothetic_density = 1f; float I = 0f; Vector2 center = Vector2.zero; Collider2D[] colliders = body.GetComponentsInChildren<Collider2D>(); for(int i = 0;i < colliders.Length; ++i) { Collider2D c = colliders[i]; //PICK ONLY OWN COMPS AND ADDITIONAL CHILD COLLIDERS THAT USE SAME BODY if(c.gameObject == body.gameObject || (c.transform.parent.gameObject == body.gameObject && c.rigidbody2D == null)) { MassData data = null; CircleCollider2D circle = c as CircleCollider2D; if(circle != null) data = CircleMassInertiaCenter(circle, hypothetic_density, body.transform.position); BoxCollider2D box = c as BoxCollider2D; if(box != null) data = BoxMassInertiaCenter(box, hypothetic_density, body.transform.position); PolygonCollider2D poly = c as PolygonCollider2D; if(poly != null) data = PolygonMassInertiaCenter(poly, hypothetic_density, body.transform.position); if(data != null) { hypothetic_mass += data.mass; center += data.center * data.mass; I += data.inertia; } } } //PORTED FROM BOX2D // Compute the center of mass. center *= 1f / hypothetic_mass; // Center the inertia about the center of mass I -= hypothetic_mass * center.sqrMagnitude; //SCALE IT I *= body.mass / hypothetic_mass; return I; }