Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best approach for scripting enemy waves

Discussion in 'Scripting' started by stuckbug, Jan 20, 2014.

  1. stuckbug

    stuckbug

    Joined:
    Jan 20, 2014
    Posts:
    2
    I'm trying to create a tower defense like wave spawner, and am looking for the best approach to make a non-random series of spawns on a set timeline. Every post or tutorial I've found on the subject seems to be based on random enemies, positions, and intervals.

    I have 3 normal enemy types, 1 boss type, and want to have them spawn in a specific pattern (i.e.: after 3 seconds spawn 1 of enemy A, after 5 seconds spawn 2 of enemy B, etc).

    Right now the spawner I created is throwing out random enemies at random intervals. I feel like I could pretty easily make a long script that has a bunch of IEnumerators set to spawn each type on each time, but that seems like a poor approach that would be error prone scale poorly.

    What I'd like is something in editor where I can add an arbitrary number of fields, one for each spawn, which specifies the enemy type and time of spawn, so I could rapidly play around with timings in editor, as opposed to needing to jump back and forth between scripts, or instead of placing a ton of individual spawner game objects. I guess I want to make an array with multiple pieces of data for every entry, or something like that.

    For reference, I'm using C# and am pretty much totally new to programming. Any advice for how I should approach this would be appreciated. Even just what I should be searching for, as I suspect I don't even know the right terminology.
     
  2. suspensemusic

    suspensemusic

    Joined:
    Dec 4, 2013
    Posts:
    41
    You could create a class for each enemy type. In that class have a variable for the amount of enemies to spawn.

    In your spawn script you could Invoke the functions in the order you want with whatever amount you want.

    This would be the way I would handle it I think
     
  3. _Nick

    _Nick

    Joined:
    Jun 29, 2013
    Posts:
    30
    How about something like...

    Code (csharp):
    1. float timer = 10;
    2. int phase = 0;
    3.  
    4. void Update()
    5. {
    6.     timer -= Time.deltaTime;
    7.     switch(phase)
    8.     {
    9.     case 0:
    10.         if(timer <= 10)
    11.         {
    12.             //spawn first set of enemies
    13.             phase = 1;
    14.         }
    15.         break;
    16.     case 1:
    17.         if(timer <= 5)
    18.         {
    19.             //spawn second set of enemies
    20.             phase = 2;
    21.         }
    22.         break;
    23.     case 2:
    24.         //etc, just continue this pattern
    25.         break;
    26.     }
    27. }
     
  4. Benjamin Magnus

    Benjamin Magnus

    Joined:
    May 23, 2013
    Posts:
    40
    Here, try out this video. I used the system laid out in it for a long time until I needed to get a little more complex. Works perfectly, and the video takes you through the whole thing line by line so its great for a new user. All the other videos on the series are good to watch too, you can learn alot of good things from them.

    http://cgcookie.com/unity/lessons/5-waves/
     
  5. stuckbug

    stuckbug

    Joined:
    Jan 20, 2014
    Posts:
    2
    Awesome, thanks for all the replies. I'll check out the video.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Just FYI - Dark Tonic just released the new version of Killer Waves, renamed to Core GameKit, in a 48-hour sale at just $5 USD.