尝试在地形上统一实例化成批的花朵

Trying to instantiate batches of flower on a terrain in unity

提问人:kayardurdass 提问时间:8/31/2023 最后编辑:kayardurdass 更新时间:9/1/2023 访问量:31

问:

所以,我有一个地形,我正在尝试在我的地形上添加小批量的花朵,我写了这段代码,但是当我启动我的游戏时,它会加载到无穷大,你能帮我吗? 这是我的代码的一部分,当我在游戏启动时将其删除时,它会实例化花朵。

    Vector2[] FlowerPatch = { };
    bool done = false;
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        animator = GetComponent<Animator>();

        TerrainData terrainData = Terrain.activeTerrain.terrainData;

        for (int i = 0; i < 1; i++)
        {
            int nFlowers = Random.Range(1, 5);
            Vector2 patchPos = new Vector2(0, 0);
            Debug.Log("Not Done");
            while (!done)
            {
                int x = Random.Range(0, 1000);
                int y = Random.Range(0, 1000);
                Vector2 pos = new Vector2(x, y);
                Debug.Log("Searching Position");
                for (int i1 = 0; i1 < FlowerPatch.Length; i1++)
                {
                    if (Mathf.Sqrt(Mathf.Pow(pos.x - FlowerPatch[i1].x, 2) + Mathf.Pow(pos.y - FlowerPatch[i1].y, 2)) > 2)
                    {
                        done = true;
                        patchPos = pos;
                        FlowerPatch.Append(pos);
                    }
                    else done = false;
                }
            }
            for (int j = 0; j < nFlowers; j++)
            {
                Vector3 FlowerPos = new Vector3(patchPos.x + Random.Range(-1.0f, 1.0f), 0, patchPos.y + Random.Range(-1.0f, 1.0f));
                FlowerPos.y = terrainData.GetHeight((int)FlowerPos.x, (int)FlowerPos.z);
                Instantiate(flower, FlowerPos, Quaternion.identity);
                Debug.Log("Instantiated Flower");
            }
        }

    }

我将不胜感激任何关于如何解决我的问题的建议:)

我尝试将我的代码移动到更新函数以降低我想要的批次数,但没有任何效果

编辑: 按照您的建议尝试了一些方法,但仍然不起作用

for (int i = 0; i < 2500; i++)
       {
           int nFlowers = Random.Range(1, 5);
           Vector2 patchPos = new Vector2(0, 0);
           Debug.Log("Not Done");
           while (!done)
           {
               int x = Random.Range(0, 1000);
               int y = Random.Range(0, 1000);
               Vector2 pos = new Vector2(x, y);
               Debug.Log("Searching Position");
               for (int i1 = 0; i1 < FlowerPatch.Length; i1++)
               {
                   if (Mathf.Sqrt(Mathf.Pow(pos.x - FlowerPatch[i1].x, 2) + Mathf.Pow(pos.y - FlowerPatch[i1].y, 2)) > 2)
                   {
                       done = true;
                       patchPos = pos;
                       break;
                   }
                   else done = false;
               }
           }
           for (int j = 0; j < nFlowers; j++)
           {
               Vector3 FlowerPos = new Vector3(patchPos.x + Random.Range(-1.0f, 1.0f), 0, patchPos.y + Random.Range(-1.0f, 1.0f));
               FlowerPatch.Append(patchPos);
               FlowerPos.y = terrainData.GetHeight((int)FlowerPos.x, (int)FlowerPos.z);
               Instantiate(flower, FlowerPos, Quaternion.identity);
               Debug.Log("Instantiated Flower");
           }
       }
unity-game-engine 实例化 unity3d-terrain

评论

0赞 BugFinder 8/31/2023
可能有很多原因。比如这段代码在花朵上,所以你会得到多个副本,但我的猜测是,如果你把一些调试放在 done 中,永远不会中断该循环,因为里面的 for 循环迭代了确切的次数,如果 done 和 for 循环的结束不重合,下次在 for 循环上再次重置为 false,因为你没有中断
0赞 kayardurdass 8/31/2023
@BugFinder,我不明白对不起,你能更准确地告诉我如何解决我的问题吗?感谢您的快速回复;)
0赞 Voidsay 8/31/2023
我觉得这可能是罪魁祸首。你对数组的长度运行循环,但在每次成功的“范围查找”(无法判断确切目的)期间,你都会向数组添加一个新元素。在我看来,这意味着 for 循环将永远运行。Append
0赞 BugFinder 9/1/2023
@kayardurdass就像我在手机上一样。查找单词 break。它在它为真的点跳出该循环(因此在本例中为 for 循环)。因此,如果您在列表中有 12000 个名字,并且您在第一个条目上找到了它,则无需检查其他 11999 个。:)

答: 暂无答案