Forum begins after the advertisement:
[Part 6] error in enemy spawner
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 6] error in enemy spawner
- This topic has 9 replies, 3 voices, and was last updated 2 months, 3 weeks ago by Terence.
-
AuthorPosts
-
December 13, 2023 at 7:52 am #12628::
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
<code>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--; } }</code>
December 13, 2023 at 12:22 pm #12635::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!
September 22, 2024 at 2:24 pm #15874::Thank you for this absolute outstanding Tutorial!:) But I have a problem: I have 4 waves with 5 Enemys per wave. The wave interval is set to 20, the max amount of enemys is set to 100. The first wave spawns fine (5 bats), but after 20 seconds, waves 2 and 3 are just skipped. Only wave 1 and 4 are spawning with this configuration. Spawn interval for each wave is set to 1. I used the latest EnemySpawner.cs and EnemyStats.cs but it won’t work as intended. Maybe someone has a solution to this, thank you in advance!
TLDR: Only the first and last wave is triggered not matter how many waves are there.
September 22, 2024 at 3:17 pm #15875::Thank you for this absolute outstanding Tutorial!:) But I have a problem: I have 4 waves with 5 Enemys per wave. The wave interval is set to 20, the max amount of enemys is set to 100. The first wave spawns fine (5 bats), but after 20 seconds, waves 2 and 3 are just skipped. Only wave 1 and 4 are spawning with this configuration. Spawn interval for each wave is set to 1. I used the latest EnemySpawner.cs and EnemyStats.cs but it won’t work as intended. Maybe someone has a solution to this, thank you in advance!
Just to clarify, after Wave 1 finishes, does it jump straight to Wave 4?
If so, for your in-between waves, check that your Wave Interval is of a large enough value. It has to be at least larger than the Spawn Interval, or the wave will be totally skipped.
September 22, 2024 at 3:31 pm #15877::Thank you for your quick reply! Yes, waves 1 finishes and then it goes straight to wave 4. I set waves interval to 30 and spawn interval of each wave to 0.5. Still the same problem.
September 22, 2024 at 4:48 pm #15879::Can you paste your
EnemySpawner
settings here as a screenshot?Alternatively, you can also consider skipping right to Part 21. The upgraded enemy spawning system is much easier to use, and is largely independent of the other parts. Since you have Bronze Patron access, you can have a look at the first 2 parts of the article to see how the new system will work: https://blog.terresquall.com/2024/07/creating-a-rogue-like-vampire-survivors-part-21/
September 22, 2024 at 5:30 pm #15882September 24, 2024 at 12:34 pm #15910::Thank you, I’ll take a look at the upgraded system then! :)
Sounds good Grim. Let me know how it goes.September 26, 2024 at 2:33 pm #15928::I found out that you already adressed this issue in part 11 (https://blog.terresquall.com/2023/07/creating-a-rogue-like-vampire-survivors-part-11/) So it works as intended right now, awesome! I’ll switch to the new enemy spawner script as soon I get to the part where you adress it.
has upvoted this post. September 27, 2024 at 7:02 pm #15932 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: