代码之家  ›  专栏  ›  技术社区  ›  SpoocyCrep

计算布雷艇爆炸

  •  1
  • SpoocyCrep  · 技术社区  · 8 年前

    我有一个体素世界,类似于Minecraft。每个体素(块)的大小为1x1x1。

    我想计算在给定爆炸半径的某个位置的爆炸破坏,这意味着在该位置周围游戏将破坏体素(给定所需的半径)。

    也就是说我想要一个能做到这一点的函数:

    void DestroyBlocks(Vector3 position, int radius){   
    if(block is on radius)   
    destroy(blockPosition);
    }
    

    我该怎么做?

    1 回复  |  直到 8 年前
        1
  •  3
  •   Bak Stak    8 年前

    这是用 Physics.OverlapSphere 功能:

    void DestroyBlocksWithinRadius(Vector3 center, float radius)
    {
        Collider[] result = Physics.OverlapSphere(center, radius);
        for (int i = 0; i < result.Length; i++)
            Destroy(result[i].gameObject);
    }
    

    如果没有碰撞器,则通过标记查找并检查距离手动执行:

    void DestroyBlocksWithinRadius(Vector3 center, float radius)
    {
        GameObject[] result = GameObject.FindGameObjectsWithTag("Voxels");
        for (int i = 0; i < result.Length; i++)
        {
            Transform tempTrans = result[i].transform;
            float distanceSqr = (center - tempTrans.position).sqrMagnitude;
            if (distanceSqr < radius)
                Destroy(tempTrans.gameObject);
        }
    }