本文整理汇总了C#中UnityEngine.Rigidbody2D.GetComponentsInChildren方法的典型用法代码示例。如果您正苦于以下问题:C# Rigidbody2D.GetComponentsInChildren方法的具体用法?C# Rigidbody2D.GetComponentsInChildren怎么用?C# Rigidbody2D.GetComponentsInChildren使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Rigidbody2D
的用法示例。
在下文中一共展示了Rigidbody2D.GetComponentsInChildren方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ComputeInertia
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;
}