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. Dismiss Notice

Enemy spawn/wave system

Discussion in 'Scripting' started by Ilkzz, Aug 13, 2013.

  1. Ilkzz

    Ilkzz

    Joined:
    Feb 22, 2013
    Posts:
    16
    Hi,

    I want to create a FPS game where enemies spawn in waves... so the first wave there might be 2/3 enemies, second wave 5/6 enemies and so on, where the enemies also get harder.

    I have created 3 times of enemies for now using cubes, spheres and cylinders

    I have a system now where when you walk passed a certain area, enemies spawn from spawn points I have placed.

    Can someone point me into the right direction on how to go about this spawn and wave system please. I have looked through numerous websites and youtube videos but Im not too sure how to do this. Im coding isnt very advanced :(

    Here is my current code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemySpawnController : MonoBehaviour {
    5.    
    6.     public static EnemySpawnController Instance;
    7.    
    8.     public GameObject Enemy;
    9.     public GameObject EnemyTwo;
    10.  
    11.     public float spawnArea = 1.5f;
    12.    
    13. void Awake() {
    14.         Instance = this;
    15. }
    16.    
    17.    
    18. public void SpawnEnemies() {
    19.     //Array
    20.     GameObject[] enemySpawnSystem = GameObject.FindGameObjectsWithTag("EnemySpawnPoint");
    21.     //loop to create enemies
    22.     int i = 0;
    23.    
    24.     foreach(GameObject spawnPoint in enemySpawnSystem)
    25.         {
    26.         int noEnemies = Random.Range(1,4);
    27.         for (int j= 0; j < noEnemies; j++)
    28.             {
    29.                 i++;
    30.                 GameObject enemy;
    31.                 enemy = Instantiate(Enemy, new Vector3(spawnPoint.transform.position.x + Random.Range(-spawnArea, spawnArea),
    32.                                                        spawnPoint.transform.position.y + Random.Range(-spawnArea, spawnArea),
    33.                                                        spawnPoint.transform.position.z + Random.Range(-spawnArea, spawnArea)),
    34.                                                        spawnPoint.transform.rotation) as GameObject;
    35.                 enemy.name = "Enemy"+i;
    36.                    
    37.             }
    38.         }
    39.     }  
    40. }
     
    Last edited: Aug 13, 2013
  2. Algie Games

    Algie Games

    Joined:
    Sep 23, 2013
    Posts:
    7
    Hey I am looking for the exact same thing, I will be checking this thread frequently to see if it gets resolved.
     
  3. Black_Eagle

    Black_Eagle

    Joined:
    Jul 19, 2014
    Posts:
    36
    Bump old post please
     
  4. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373

    Well here's a basic system all typed outside of unity so probably a few errors but basically should work/give you ideas


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic; //for lists and generics..
    5.  
    6. public class EnemySpawnController : MonoBehaviour {
    7.  
    8.     public static EnemySpawnController Instance;
    9.  
    10.    //Enemy Types should probably be a list for easier additions later on
    11.       public List<GameObject> enemyTypes = new List<GameObject>(); // add via inspector as you did before below
    12.    // public GameObject Enemy;
    13.    // public GameObject EnemyTwo;
    14.  
    15.     public float spawnArea = 1.5f;
    16.     //new vars... make public/private as you will... doing public here for ease of use in inspector
    17.     public float timeBetweenWaves = 15.0f; // give you 15 sec for default
    18.     public float timeBetweenSpawns = 0.5f; // half sec between enemy spawns
    19.     public int wave = 1; // start at wave 1
    20.     public int startEnemyCount = 3; // number of enemies... our base amount
    21.     public int addPerWave = 2; // how many new enemies per wave
    22.  
    23.     private int enemyCount = 0; // active enemies
    24.     private int enemySpawned = 0;
    25.  
    26. void Awake() {
    27.         Instance = this;
    28. }
    29.  
    30. void Start(){
    31. //Don't do this per spawn... it's been moved to start
    32.      GameObject[] enemySpawnSystem = GameObject.FindGameObjectsWithTag("EnemySpawnPoint");
    33.  
    34.      Invoke("StartWave", timeBetweenWaves);
    35. }
    36.  
    37. void StartWave(){
    38.      InvokeRepeating("SpawnEnemies" timeBetweenSpawns, timeBetweenSpawns);
    39. }
    40.  
    41. void EndWave(){
    42.     wave++;
    43.     enemyCount = 0;
    44.     enemySpawned = 0;
    45.     Invoke("StartWave", timeBetweenWaves);
    46. }
    47.  
    48. void CheckEnemyCount(){
    49.     if(enemySpawnedt >= (startEnemyCount + wave * addPerWave)){
    50.          CancelInvoke("SpawnEnemies");
    51.     }
    52.  
    53. // be sure to have your enemy counter decrease when you kill enemies or this will never work
    54.    if(enemyCount < 1){
    55.          EndWave();
    56.    }
    57.  
    58. }
    59.  
    60.  
    61. public void SpawnEnemies() { // should be named spawn enemy since we will do singular enemies
    62.   // you spawned enemies according to spawn points... not the way to go if you want number of enemies/wave
    63.   // changed this function
    64. int rand = Random.Range(0, enemySpawnSystem.count - 1); // grab a random num
    65. Transform spawn =  enemySpawnSystem[rand].transform; // grab a spawn point position
    66. rand = Random.Range(0, enemyTypes.Length - 1);
    67.    
    68. GameObject enemy = Instantiate(enemyTypes[rand], spawn.transform.position.x +
    69.                                                        Random.Range(-spawnArea, spawnArea),  spawn.transform.position.y +
    70.                                                        Random.Range(-spawnArea, spawnArea),
    71.                                                        spawn.transform.position.z + Random.Range(-spawnArea, spawnArea)),
    72.                                                        spawn.transform.rotation) as GameObject;
    73.                 enemy.name = "Enemy"+i;
    74.  
    75.      enemySpawned++;
    76.      enemyCount++; // add an enemy when it's spawned
    77.      CheckEnemyCount(); // check on our enemies
    78. }
    79.  
    80. }
    81. }
     
    Last edited: Aug 7, 2014
  5. Black_Eagle

    Black_Eagle

    Joined:
    Jul 19, 2014
    Posts:
    36
  6. nitro0198

    nitro0198

    Joined:
    Dec 11, 2020
    Posts:
    6


    i tried to follow this script but i have some error in the SpawnEnemis void, why?
     

    Attached Files:

  7. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Not sure if you noticed, but the original post is from 2013. You should probably start a new one if you want to get anyone to help you out with this.
     
    Bunny83 likes this.
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Why? because the script that novashot posted contains tons of errors and never worked in the first place. Just a few example:

    • He declared the "enemySpawnSystem" variable inside Start, so the variable only exists inside the Start method.
    • The generic List class does not have a field / property called
      count
      , it's called
      Count
      . C# is case sensitive.
    • He subtract one from the upper bound of the Random.Range method which is wrong as the upper bound is exclusive when used with integer arguments. So the last element can never be choosen. If the list / array only has one element, it would even cause an out of bounds error
    • He has several logical issues. For example he wants to end a wave once all enemies has been destroyed based on a comment in his code. However the code that checks the number of active enemies only executes after an enemy has been spawned. So once all enemies of the wave are there, he cancels the invokerepeating call so that code is no longer executed. Therefore it can never advance to the next wave.
    • The code has a few bad habits. For example the local "spawn" variable is of type transform. Though later he uses
      spawn.transform
      which is redundant and just adds overhead and confusion.
    Apart from all that, like @jbnlwilliams1 already said, this is an ancient thread. If you have an issue with your code, start a new thread. We already have a few monster threads with literally dozens of pages of posts. Such threads are a mess as it's almost impossible to find any relevant information and the risk is high that the same things are addressed multiple times because nobody will take the time to look through hundreds of post. So as a general rule: Keep threads short, if they get too long, they fail their purpose. Don't necro-post. This is a general forum rule. Feel free to start a new thread with a link to the original thread. Therefore relevant information can be looked up if necessary.
     
    Kurt-Dekker likes this.