How to Create an Enemy Wave System in Unity

Overview:Today we will be going over how to create an Enemy Wave System in Unity.Wave System:For the wave system we created a new script and attached it to the spawn manager.First, we get reference to the spawn manager script and UI for use later. We n…


This content originally appeared on Level Up Coding - Medium and was authored by Christopher Adams

Overview:

Today we will be going over how to create an Enemy Wave System in Unity.

Wave System:

For the wave system we created a new script and attached it to the spawn manager.

  1. First, we get reference to the spawn manager script and UI for use later. We need an int for the current wave, we also need one for the number of enemies to spawn and also for how many enemies our left. We also create a Bool to know if it’s the start of a wave.

2. Inside the start method we can use GameObject.Find to grab UI Manager script from the Canvas. We also need to get the Spawn Manager. Inside the Update method we check if the enemies left is less than or equal to zero and that the start of wave is equal false. If it’s true we set the enemies left to zero to keep from going negative and call the end wave method.

3. We have a public method called start wave that sets the startOfWave true and starts the wave Coroutine. Inside the IEnumerator, we call the Update wave display method on the UI script and pass in the current Wave. Then we wait 3 seconds and check if the enemies left are not equal to the enemies to spawn. If true, then we call the start spawning method on the Spawn Manager.

4. We also have a Public End Wave method that starts the End wave coroutine. Inside the IEnumerator we set the start of wave true, increase the current wave by one and add five to the enemies to spawn. Then we wait two and a half seconds and call the start wave method.

Update UI Show Wave:

  1. Inside the UI Manager script. We need to create a new TextMeshProUGUI variable for our wave display text.
  2. Next, we Create a new public method for enabling the text and displaying the current wave number.

Spawning The Enemies:

Inside the Spawn Manager, we simply modified the spawning couratine. We create a new local int variable that we increment once every time an enemy is spawned. If the number of enemies spawned is equal to the wave managers enemies to spawn variable, then we stop spawning, set the start of wave false, and reset the enemies spawned to zero.

Destroying Enemies:

Inside the enemy script, we need to create a new variable to grab the wave manager class. Inside the start method, we can use the game object.Find method to find the spawn manager inside the scene and grab the Wave manager script. Now inside the outrigger enter method before we destroy the enemy. We decrease the enemies left by one.

We should now have the enemies spawn in waves.

That’s it for this article.

Thank you for your time and attention.

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Placing developers like you at top startups and tech companies


How to Create an Enemy Wave System in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Christopher Adams


Print Share Comment Cite Upload Translate Updates
APA

Christopher Adams | Sciencx (2022-09-22T15:35:43+00:00) How to Create an Enemy Wave System in Unity. Retrieved from https://www.scien.cx/2022/09/22/how-to-create-an-enemy-wave-system-in-unity/

MLA
" » How to Create an Enemy Wave System in Unity." Christopher Adams | Sciencx - Thursday September 22, 2022, https://www.scien.cx/2022/09/22/how-to-create-an-enemy-wave-system-in-unity/
HARVARD
Christopher Adams | Sciencx Thursday September 22, 2022 » How to Create an Enemy Wave System in Unity., viewed ,<https://www.scien.cx/2022/09/22/how-to-create-an-enemy-wave-system-in-unity/>
VANCOUVER
Christopher Adams | Sciencx - » How to Create an Enemy Wave System in Unity. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/22/how-to-create-an-enemy-wave-system-in-unity/
CHICAGO
" » How to Create an Enemy Wave System in Unity." Christopher Adams | Sciencx - Accessed . https://www.scien.cx/2022/09/22/how-to-create-an-enemy-wave-system-in-unity/
IEEE
" » How to Create an Enemy Wave System in Unity." Christopher Adams | Sciencx [Online]. Available: https://www.scien.cx/2022/09/22/how-to-create-an-enemy-wave-system-in-unity/. [Accessed: ]
rf:citation
» How to Create an Enemy Wave System in Unity | Christopher Adams | Sciencx | https://www.scien.cx/2022/09/22/how-to-create-an-enemy-wave-system-in-unity/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.