Пример #1
0
    IEnumerator Spawn()
    {
        yield return(new WaitForSeconds(initialDelay));

        if (spawners.Length == 0 || prefabsToSpawn.Length <= 0)
        {
            Debug.LogError("A spawner needs SpawnerLocations added to it, Planter prefabs set and a positive interval!");
        }
        else
        {
            while (true)
            {
                if (currentNumberSpawned >= maxSpawns)
                {
                    currentNumberSpawned = 0;
                    yield return(new WaitForSeconds(Random.Range(90, 300)));
                }
                else
                {
                    var  spawner       = this.spawners[Random.Range(0, this.spawners.Length)];
                    var  spawnLocation = spawner.transform.position;
                    int  numTries      = 0;
                    bool found         = false;

                    while (numTries < MAX_SPAWN_TRIES && !found)
                    {
                        var     randCircle = Random.insideUnitCircle * spawner.Radius;
                        Vector3 rayStart   = spawnLocation + new Vector3(randCircle.x, 0, randCircle.y);
                        rayStart.y = 500.0f;

                        RaycastHit hitInfo;
                        Ray        ray = new Ray(
                            rayStart,
                            Vector3.down
                            );
                        if (Physics.Raycast(ray, out hitInfo, 1000.0f) && hitInfo.collider.gameObject.layer == islandLayer)
                        {
                            var prefab = prefabsToSpawn[Random.Range(0, this.prefabsToSpawn.Length)];
                            var go     = GameObject.Instantiate(prefab, hitInfo.point + new Vector3(0.0f, 0.01f, 0.0f), Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up));
                            go.AddComponent <SpawnedTracker>().SetParentSpawner(this);
                            currentNumberSpawned++;
                            found = true;
                        }
                        else
                        {
                            numTries++;
                        }
                    }

                    if (!found)
                    {
                        Debug.LogWarning("Trouble spawning at: " + spawnLocation.ToString() + " tries: " + numTries.ToString());
                    }

                    float interval = Mathf.Max(0.01f, intervalRange.Lerp(currentNumberSpawned / (float)maxSpawns));
                    yield return(new WaitForSeconds(interval));
                }
            }
        }
    }
Пример #2
0
        void Awake()
        {
            // Get a random number in amountRange
            int amount = amountRange.RandomInclusive;

            // Output [amount] numbers
            for (int i = 0; i < amount; i++)
            {
                // Transform [i..(amount-1)] to [0..1]
                var t = ((float)i / (amount - 1));

                // Mathf.Lerp(numberRange.From, numberRange.To, t)
                Debug.Log(numberRange.Lerp(t));
            }
        }