【问题标题】:Can't get AddExplosionForce to work with children of game object无法让 AddExplosionForce 与游戏对象的子级一起使用
【发布时间】: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


    【解决方案1】:

    我刚刚想通了。我需要添加 ForceMode 以获得我正在寻找的爆炸。我最终使用了 ForceMode.Impulse,效果非常好。

    【讨论】:

    • 不要在答题部分...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多