Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Wave system for 2d game

Discussion in '2D' started by Elrien, Dec 12, 2015.

  1. Elrien

    Elrien

    Joined:
    Jul 15, 2014
    Posts:
    32
    Hi everyone, i'm trying to make a 2D game in which the player has to kill a number of enemies and get the goal. when he does, the goal is randomly spawned again and around him another wave of enemies that start moving in the player's direction. i was looking for something i could use on the internet, but nothing really works the way i would like. i tried to use this code i wrote following a tutorial:
    Code (CSharp):
    1.     public Wave[] waves;
    2.     public Pirate pirate;
    3.  
    4.     Wave currentWave;
    5.     int currentWaveNumber;
    6.  
    7.     int piratesRemainingToSpawn;
    8.     int piratesRemainingAlive;
    9.     float nextSpawnTime;
    10.  
    11.     void Start() {
    12.         NextWave();
    13.     }
    14.  
    15.     void Update() {
    16.         if(piratesRemainingToSpawn > 0 && Time.time > nextSpawnTime) {
    17.             piratesRemainingToSpawn --;
    18.             nextSpawnTime = Time.time + currentWave.timeBetweenSpawn;
    19.        
    20.             Pirate spawnedPirate = Instantiate(pirate, Vector3.zero, Quaternion.identity) as Pirate;
    21.             }
    22.         if(piratesRemainingAlive == 0) {
    23.             NextWave();
    24.         }
    25.     }
    26.  
    27.     void NextWave() {
    28.         currentWaveNumber ++;
    29.         print("Wave: " + currentWaveNumber);
    30.         if(currentWaveNumber - 1 < waves.Length) {
    31.             currentWave = waves[currentWaveNumber -1];
    32.             piratesRemainingToSpawn = currentWave.pirateCount;
    33.             piratesRemainingAlive = piratesRemainingToSpawn;
    34.         }
    35.     }
    36.  
    37.  
    38.     [System.Serializable]
    39.     public class Wave {
    40.         public int pirateCount;
    41.         public float timeBetweenSpawn;
    42.     }
    43. }
    and the first wave is spawned okay. the second is not spawned at all and i think it's because i don't keep track of the enemies that are killed. since i have a OnTriggerEnter2D on my player script that destroys the enemies i thought of keeping track there and than using the number in the wave script. but i'm not sure how or where to use it to modify the current script. can you help me? even by posting a different wave system, that would be great! thank you:)
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Logically, The pirates remaining alive should be the count of your pirates so when they equal 0 you will start to spawn the next wave but you will also need to reset the pirates remaining to spawn as well. Once that's done it should go back to your normal update & spawn.
     
    Elrien likes this.
  3. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    Break your problem into parts: have a alivePirates variable somewhere, then let your PlayerScript (or PirateScript) decrease that variable when a pirate is killed.

    To gain access to such a variable, there are different ways.. Simplest is to use a global static variable. In your WaveScript (or anywhere, maybe GlobalState.cs, GameManager.cs, doesn't really matter) you can make your alivePirates variable static and public, or make it private and expose a method to decrement it. When a property or method is declared "public static", you can access it from anywhere within the namespace by calling Classname.alivePirates for example

    Some people will scream "oh my god public static state!", in which case you can either use some sort of a messaging or event system to broadcast an event when you kill a pirate or use a singleton pattern for a gamemanager class (which is honestly pretty much the same thing as just having a static variable)
     
    Elrien likes this.
  4. Elrien

    Elrien

    Joined:
    Jul 15, 2014
    Posts:
    32
    Thank you very much to both of you, it worked fine :)