Forum begins after the advertisement:


[Part 6] error in enemy spawner

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #12628
    a
    Former Patron

    permission to ask sir, I am still experiencing errors in enemy spawn. I made 20 waves but when I beat all the first waves I couldn’t go to the next wave. Here I don’t understand the explanation in the video. Can you explain it in more detail? Thank you for your help

    Screenshot-245

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EnemySpawner : MonoBehaviour
    {
        [System.Serializable]
        public class Wave
        {
            public string waveName;
            public List<EnemyGroup> enemyGroups;    //A list of groups of enemies to spawn in this wave
            public int waveQuota;   //The total number of enemies to spawn in this wave
            public float spawnInterval; //The interval at which to spawn enemies
            public int spawnCount;  //The number of enemies already spawned in this wave
        }
    
        [System.Serializable]
        public class EnemyGroup
        {
            public string enemyName;
            public int enemyCount;  //The number of enemies of this type to spawn in this wave
            public int spawnCount;  //The number of enemies of this type already spawned in this wave
            public GameObject enemyPrefab;
        }
    
        public List<Wave> waves;    //A list of all the waves in the game
        public int currentWaveCount;    //The index of the current wave [Remember, a list starts from 0]
    
        [Header("Spawner Attributes")]
        float spawnTimer; //Timer used to determine when to spawn the next enemy
        public int enemiesAlive;
        public int maxEnemiesAllowed; //The maximum number of enemies allowed on the map at once
        public bool maxEnemiesReached = false;  //A flag indicating if the maximum number of enemies has been reached
        public float waveInterval;  //The interval between each wave
    
        [Header("Spawn Positions")]
        public List<Transform> relativeSpawnPoints; //A list to store all the relative spawn points of enemies
    
        Transform player;
    
        void Start()
        {
            player = FindObjectOfType<PlayerStats>().transform;
            CalculateWaveQuota();
        }
    
        // Update is called once per frame
        void Update()
        {
            if (currentWaveCount < waves.Count && waves[currentWaveCount].spawnCount == 0)  //Check if the wave has ended and the next wave should start
            {
                StartCoroutine(BeginNextWave());
            }
    
            spawnTimer += Time.deltaTime;
    
            //Check if it's time to spawn the next enemy
            if (spawnTimer >= waves[currentWaveCount].spawnInterval)
            {
                spawnTimer = 0f;
                SpawnEnemies();
            }
        }
    
        IEnumerator BeginNextWave()
        {
            //Wait for <code>waveInterval</code> seconds before starting the next wave.
            yield return new WaitForSeconds(waveInterval);
    
            //If there are more waves to start after the current wave, move on to the next wave
            if (currentWaveCount < waves.Count - 1)
            {
                currentWaveCount++;
                CalculateWaveQuota();
            }
        }
    
        void CalculateWaveQuota()
        {
            int currentWaveQuota = 0;
    
            foreach (var enemyGroup in waves[currentWaveCount].enemyGroups)
            {
                currentWaveQuota += enemyGroup.enemyCount;
            }
    
            waves[currentWaveCount].waveQuota = currentWaveQuota;
            Debug.LogWarning(currentWaveQuota);
        }
    
        /// <summary>
        /// This method will stop spawning enemies if the amount of enemies on the map is maxmimum.
        /// The method will only spawn enemies in a particular wave until it is time for the next wave's enemies to be spawned
        /// </summary>
        void SpawnEnemies()
        {
            // Check if the minimum number of enemies in the wave have been spawned
            if (waves[currentWaveCount].spawnCount < waves[currentWaveCount].waveQuota && !maxEnemiesReached)
            {
                // Spawn each type of enemy until the quota is filled
                foreach (var enemyGroup in waves[currentWaveCount].enemyGroups)
                {
                    // Check if the minimum number of enemies of this type have been spawned
                    if (enemyGroup.spawnCount < enemyGroup.enemyCount)
                    {
                        // Limit the number of enemies that can be spawned at once
                        if (enemiesAlive >= maxEnemiesAllowed)
                        {
                            maxEnemiesReached = true;
                            return;
                        }
    
                        // Spawn the enemy at a random position close to the player
                        Instantiate(enemyGroup.enemyPrefab, player.position + relativeSpawnPoints[Random.Range(0, relativeSpawnPoints.Count)].position, Quaternion.identity);
                        
                        enemyGroup.spawnCount++;
                        waves[currentWaveCount].spawnCount++;
                        enemiesAlive++;
                    }
                }
            }
            //Reset the maxEnemiesReached flag if the number of enemies alive has dropped below the maximum allowed
            if (enemiesAlive < maxEnemiesAllowed)
            {
                maxEnemiesReached = false;
            }
        }
    
        // Call this function when an enemy is killed
        public void OnEnemyKilled()
        {
            //Decrement the number of enemies alive
            enemiesAlive--;
        }
    }
    #12635
    Terence
    Keymaster

    Hi HAKI, there are a few things you want to adjust to make your waves last longer:

    • You want to increase the Wave Interval value to a larger number. As a rule of thumb, your Wave Interval should always be much higher than your Spawn Interval of each group. This is because whenever the Wave Interval is reached, the spawner will move on to the next wave. Because your Spawn Interval is 4, and the Wave Interval is 1, there will be some waves that do not spawn any enemies at all. I recommend setting the Wave interval to a larger value like 50.
    • Make sure that your Max Enemies Allowed is of a high enough value. If it is too low, enemies will stop spawning (and the wave will continue to advance). I would set it to 100.
    • In your Game Manager, make sure that your Time Limit is large enough, otherwise the game will end prematurely. For example, if you have 20 waves and a Wave Interval of 40, you will need at least 800 seconds for the Time Limit (though you will want to give the player a little bit more time, otherwise the game will end as soon as the last wave spawns).

    Hope the explanation helps!

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: