c# - 计算 Minecraft 爆炸

标签 c# unity3d

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

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

意思是我想要某种功能来做到这一点:

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

我该怎么做?

最佳答案

这是通过 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);
    }
}

关于c# - 计算 Minecraft 爆炸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51516088/

相关文章:

c# - 内存重新排序会导致 C# 访问未分配的内存吗?

c# - DateTime.Today 和 "static readonly"

c# - 在 Express Checkout 审核页面中显示订单项

c# - Unity 2018.3 HDRP - 以编程方式更改 Material 颜色?

c# - LINQ-to-SQL 编译查询问题(用作未编译查询)

c# - unity SQLite 本地数据库

c# - RigidBody2D 卡住 X 位置

ios - iTunesConnect 在提交审核时不显示其他语言

c# - 面板在 NGUI 中隐藏和显示

c# - 将 IHttpActionResult 序列化为 JSON 会导致异常