Search Unity

making enemy waves

Discussion in 'Scripting' started by thebest07111, Oct 30, 2014.

  1. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    hello

    I have a problem and i dont know how to make it.
    For school we need to create an sideshooter. We have done anything except the enemy waves.

    We have 10 enemy's. And what i want to do is have a script. that you can set the waves into with like 4 spawnpoint in it that it randomly spawns in. but you can also add more enemy's per wave. and that you can also add more waves. Because we need like 20 waves.

    Does someone has a script for that because i dont know where to start?

    hope you guys can help
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Code (CSharp):
    1. public List<GameObject> enemies = new List<GameObject>(); // my list of baddies
    2. public List<Transform> spawnPoints = new List<Transform>(); // my list of spawn points
    3. public int wave = 1; // start at wave 1 presumably
    4. private int enemiesSpawnedThisWave = 0;
    5. private int enemyTotalThisWave = 0;
    6. public int baseEnemyCount = 3; // base enemy amount
    7. public int additionalEnemies = 10; // extra enemies per wave
    8. public float timeBetweenWaves = 5.0f; // 5 sec
    9. public float timeBetweenSpawns = 0.5f; // half sec
    10.  
    11. void Start(){
    12.      Invoke("StartWave", timeBetweenWaves);
    13. }
    14.  
    15. void StartWave(){
    16.    enemyTotalThisWave = baseEnemyCount + (wave * additionalEnemies); // add any variation here to spice it up
    17.    InvokeRepeating("Spawn", timeBetweenSpawns);
    18. }
    19.  
    20. void Spawn(){
    21.    // your spawn code(instantiate / pool system
    22.    // use random range to pick random enemy / spawn point
    23.  
    24. enemiesSpawnedThisWave++;
    25.  
    26. if(enemiesSpawnedThisWave >= enemyTotalThisWave){
    27.    CancelInvoke("Spawn");
    28.    wave++;
    29.    enemiesSpawnedThisWave = 0;
    30.  
    31.    Invoke("StartWave", timeBetweenWaves);
    32.    }
    33. }

    very basic implementation all written in the browser so be aware it may contain an error or 2... but is meant to be more of a guide anyway...
     
  3. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    Thanks you so much i will take a look at it.

    In your code it says pool system. what do you mean with that?

    And in what script do you putt this because i think it isnt't C# because you dont have public class monobehaviour?
     
    Last edited: Oct 30, 2014
  4. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Do a search on pools you'll see plenty... basically a recycling system ratyer than create / destroy. This is c# but not a full class definition so no inheriting from monobehaviour... these could be integrated into your current spawning class or used to start a new one. I just wanted to show one possible way to do what you were looking to do.