Trying to instantiate batches of flower on a terrain in unity

37 Views Asked by At

So, I have a terrain and I'm trying to add little batches of flowers all over my terrain I wrote this code but when I launch my game it loads to infinity can you help me? This is the part of my code that instantiate the flowers when I remove it the game launch.

    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");
            }
        }

    }

I would appreciate any advice on ow to solve my problem :)

I tried moving my code to the update function lowering the number of batches I want but nothing works

EDIT: tried some things as you suggested but it still doesn't work

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");
           }
       }
0

There are 0 best solutions below