【发布时间】:2021-11-26 20:42:53
【问题描述】:
我有一个有几个孩子的游戏对象。我试图将孩子与父母分开,并根据父母身上的位置对每个孩子施加爆炸力。孩子们分离得很好,父母被移除了(我在游戏运行时检查了场景)我检查了哪些游戏对象靠近爆炸位置,它们打印出来了。我只是无法让爆炸力对它们起作用。每个孩子都有一个刚体(在预制件中设置),我在代码中添加了一个盒子碰撞器。我尝试了更大的爆炸半径,但也没有用。
private void OnCollisionEnter(Collision collision)
{
Vector3 pos = transform.position;
for(int i=0;i<transform.childCount;)
{
Transform childTransform = transform.GetChild(0);
Debug.Log("C"+childTransform.transform.position);
childTransform.parent = null;
childTransform.gameObject.AddComponent<BoxCollider>();
}
Collider[] colliders = Physics.OverlapSphere(pos, 10);
foreach (Collider childGameObject in colliders)
{
Debug.Log(childGameObject.name);
childGameObject.GetComponent<Rigidbody>().AddExplosionForce(10f, pos, 10f, 10f);
//childGameObject.GetComponent<Rigidbody>().AddExplosionForce(300f, pos, 300f, 10f);
}
Destroy(gameObject);
}
【问题讨论】:
标签: unity3d game-physics