Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by kevinspawner · Jun 26, 2014 at 07:59 AM · enemyenemy spawnwave

Enemy Wave Generator Spawn System Help!

Am developing a spaceShooter(3D Game) and got in to a wall. Am trying to create a enemy Spawn waves which will spawn 3 different types of enemies as listed below, infinitely. There will be 4 waves, each wave will spawn enemy prefabs in a timely manner. Once all the four waves get completed again it get looped so basically it will be infinite 4 waves and speed of spawn increases after each 4 waves.

Remember please am in a learning curve, I need an efficient way to structure the below logic.. I knew it is not an efficient manner and would like to structure the concept in an efficient manner. I do have done lot of research and found many wave generators most of which is not similar to the one am looking for... So, please help me out here. Thanks a lot for your time and Happy Coding!

 enemy prefabs
 basicEnemyShip prefab
 mediumEnemyShip Prefab
 Boss EnemyShip Prefab
 
 Waves of enemies
 
 First Wave
 Second Wave
 Third Wave
 Fourth Wave
 
 First Wave
 {
   Timer 20 Sec 
   Instantiate basic enemy prefab(N Numbers)
   Timer 25 Sec
   Instantiate mediumEnemyShip (N Numbers)
   Timer 30 Sec
   Instantiate BossEnemyShip Prefab
 }
 
 Second Wave
 {
   Timer 30 Sec
   Instantiate basic enemy prefab (N numbers)
   Timer 35 Sec
   Instantiate Medium EnemyShip (N Numbers)
   Timer 30 Sec
   Instantiate BossEnemyShip Prefab
 }
 
 Third Wave
 {
   Timer 40 Second
   Instantiate basic enemy prefab + mediumEnemyShip (N numbers Random Numbers)
   Timer 30 Second
   Instantiate bossEnemyShip Prefab
 
 }
 
 Fourth Waves
 {
   Timer 50 Second
   Instantiate mediumEnemyShip+BossEnemyShip
   Timer 40 Second
   Instantiate BossEnemyShip
 }
 
 Wave loops
 {
   First Wave
   
   Second Wave
   
   Third Wave
   
   Fourth Wave
 }
 
 
 Enemy spawn Amount increases end of each loops (4 waves)
 enemy speed increases end of each loops (4 waves)


Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rohitanshu · Jun 26, 2014 at 08:04 AM 1
Share

Have you tried unity's tutorial project Space Shooter?

avatar image kevinspawner · Jun 26, 2014 at 08:11 AM 0
Share

Thanks for your message. Yeah! I did tried rohitanshu, I need an efficient structure to loop this using coroutine. Problem is in that tutorial it is just some basic Ienumarator explained. But I need an proper structure for my logic. Hope I make a sense here.

avatar image rohitanshu · Jun 26, 2014 at 11:58 AM 0
Share

Yeah, you do. ;) Then I think Bunny83's answer would suffice. Of course, you should read it, learn it and tweak it according to your needs. All the best. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by Bunny83 · Jun 26, 2014 at 09:30 AM

I just had nothing else to do, so i wrote a simple wave generator ;)

 // C#
 // WaveGenerator.cs
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class WaveAction
 {
     public string name;
     public float delay;
     public Transform prefab;
     public int spawnCount;
     public string message;
 }
 
 [System.Serializable]
 public class Wave
 {
     public string name;
     public List<WaveAction> actions;
 }
 
 
 
 public class WaveGenerator : MonoBehaviour
 {
     public float difficultyFactor = 0.9f;
     public List<Wave> waves;
     private Wave m_CurrentWave;
     public Wave CurrentWave { get {return m_CurrentWave;} }
     private float m_DelayFactor = 1.0f;
 
     IEnumerator SpawnLoop()
     {
         m_DelayFactor = 1.0f;
         while(true)
         {
             foreach(Wave W in waves)
             {
                 m_CurrentWave = W;
                 foreach(WaveAction A in W.actions)
                 {
                     if(A.delay > 0)
                         yield return new WaitForSeconds(A.delay * m_DelayFactor);
                     if (A.message != "")
                     {
                         // TODO: print ingame message
                     }
                     if (A.prefab != null && A.spawnCount > 0)
                     {
                         for(int i = 0; i < A.spawnCount; i++)
                         {
                             // TODO: instantiate A.prefab
                         }
                     }
                 }
                 yield return null;  // prevents crash if all delays are 0
             }
             m_DelayFactor *= difficultyFactor;
             yield return null;  // prevents crash if all delays are 0
         }
     }
     void Start()
     {
         StartCoroutine(SpawnLoop());
     }
 
 }

This allows you to define as many waves you like and each wave can have mutiple spawn actions each with a custom delay. You can also spawn multiple different prefabs at the same time by setting the delay of subsequent actions to 0. You can also use this to just print a message by not assigning a prefab but only a message.

It's a quite "hacky" solution but quite versatile and easy to extend.

Here's how it looks like in the inspector where you would define your waves:

Wave generator edit
I forgot that you wanted to lower the delay each iteration. I've added a simple factor which is lowered each iteration by 10% (times 0.9). So during the first iteration the factor is 1.0 during the next iterations it will be 0.9 | 0.81 | 0.729 | ... always 10% of the previous iteration


wavegenerator.png (44.6 kB)
Comment
Add comment · Show 25 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 26, 2014 at 09:33 AM 1
Share

ps: when editing the waves keep in $$anonymous$$d that you can duplicate or delete array elements in the inspector. That simplifies editing since you can duplicate whole waves.

avatar image kevinspawner · Jun 26, 2014 at 09:45 AM 0
Share

Bunny! Thanks a million...! Highly Appreciated!. I, never expected someone would be this helpful.I will check it now and will let you know (If I need any explanation from this code and how it works!) Thanks a lot.

avatar image kevinspawner · Jun 26, 2014 at 10:15 AM 0
Share

Since am in a learning curve this system looks quiet complicated. However I do have a question (might be a stupid question! :))

  1. Is it possible to add my own prefab example like basicEnemyPrefab declare it and then can instantiate it inside the loop that you have created? can you please write an example code so I would get the idea. Just for one object if you can explain for one prefab I would get the idea! Thanks again sorry for shooting with multiple questions!

avatar image Bunny83 · Jun 26, 2014 at 10:24 AM 0
Share

Uhm that's the purpose of the whole thing ;) You have to assign your prefabs in the inspector for each "spawn action" to the corresponding "prefab" variable. I haven't included the actual instantiate code since that depends on your map / scene.

There are two "TODOs" in the code. One is the instantiation of the prefab, the other is the ingame message. If you don't need the message you can simply ignore that ;)

I can highlight where you would assign your prefabs in the image, just a second..

edit
I added the lowering of the spawn delay each iteration.

avatar image rohitanshu · Jul 09, 2015 at 06:16 PM 1
Share

@NEWBIE_1 : Yes it can be. It's actually structured for a 3D game (see the question, plus how Vector3 is used). But you'd have to tweak it somewhat according to you specific needs.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning Waves: Second wave doesn't start? 0 Answers

enemy wave spawn script wont work 0 Answers

Enemy ia fliping when hitting object 2 Answers

Setting up a wave system 1 Answer

simple waypoint system enemy wave spawner 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges