TD Wave spawning

Discussion in 'Scripting' started by StefanB95, Apr 8, 2011.

  1. StefanB95

    StefanB95

    New Member

    Joined:
    Mar 14, 2011
    Messages:
    97
    Hello all i just wanna ask if someone can help me a wave spawning script like in tower defense games.I'm making a game like this and i need to spawn a wave of creeps and when they are dead wait some time and spawn another wave and that all till the end of the game. No need to write the script if you don't want 2 just to help me make it thx :)
  2. Kokumo

    Kokumo

    New Member

    Joined:
    Jul 23, 2010
    Messages:
    390
    Nice game!
    First of all, you have to have a prefab of a monster to be cloned.
    The idea is instantiate several of your monsters when the last of a previous wave, died... so, the second thing you have to got is an emptyGameObject (for example) with the script we have to make right now, attached.

    Create a script (call it WaveSpawn), the create a emptyGameObject (call it WaveSpawnOBJ), and attach WaveSpawn to WaveSpawnOBJ.

    We need to know how many creature we are going to create, how many still alive, and how much time we have to wait before a new wave will appear, and a counter for that time; so, we need some variables:
    - totalCreatures
    - livesCreatures
    - waitToWave
    - waveTime


    Code (csharp):
    1.  
    2. int totalCreatures = 17; //I set 17 because i like 17
    3. int livesCreatures = 0; //remaining creatures; this number will vary through the game
    4. float waitToWave = 10; //we will wait 10 second for the next wave, after the las creature
    5. float waveTime; //remaining time to reach waitToWave;
    6. Transform yourCreature; //this must be public to set your creature prefab (in the editor)
    7.  
    8. function Update() {
    9.  
    10.     if (livesCreatures == 0) { //the is no creature left; create a wave!
    11.  
    12.         if (waitToWave <= waveTime) {
    13.             waveTime = 0; //reset time
    14.             livesCreatures = totalCreatures; //fill the remaining creatures variable
    15.  
    16.             for (int i = 0; i < totalCreatures; i++) //Create a creature (as many to reach totalCreatures)
    17.                 Instantiate (yourCreature, Vector3(0,0,0), Quaternion.identity);
    18.  
    19.         }else {
    20.             waveTime += Time.DeltaTime; //could fail xD!
    21.         }
    22.     }
    23. }
    24.  
    What it does?
    First, check if an enemy is alive; if it is... it just do... nothing.
    In other case, the script waits for 10 second to instantiates the creatures.

    Of course, this code was not tested...

    PS: i forgot to mention that each creature, when diying, have to discount 1 to livesCreatures...
    Last edited: Apr 8, 2011
  3. StefanB95

    StefanB95

    New Member

    Joined:
    Mar 14, 2011
    Messages:
    97
    Can i spawn with this script more prefab monster or just one. I mean with it that i can get more art of monster spawning like weak, strong or just one monster prefab
  4. Kokumo

    Kokumo

    New Member

    Joined:
    Jul 23, 2010
    Messages:
    390
    This only allows you to instantiate one kind of monster, which is definen by yourCreature variable; you will need to make some changes to achieve waves with differents monsters.

    NOTE: is c# so, you will need to create a c# class.
    Last edited: Apr 8, 2011
  5. StefanB95

    StefanB95

    New Member

    Joined:
    Mar 14, 2011
    Messages:
    97
    I got a error on the script or don;t now it says
  6. Kokumo

    Kokumo

    New Member

    Joined:
    Jul 23, 2010
    Messages:
    390
    Are you using the script editor of unity?
    - create a c3 class.
    - paste inside the existing update function of the class, the content of the update function that i posted.
    - finally, copy the variables declaration of this post, above the update function of the class.
  7. badbii

    badbii

    New Member

    Joined:
    Dec 6, 2009
    Messages:
    83
    script without any mistakes in coding:
    Code (csharp):
    1. var totalCreatures : int = 17; //I set 17 because i like 17
    2. var livesCreatures : int  = 0; //remaining creatures; this number will vary through the game
    3. var waitToWave : float = 10; //we will wait 10 second for the next wave, after the las creature
    4. var waveTime: float; //remaining time to reach waitToWave;
    5. var yourCreature :Transform;  //this must be public to set your creature prefab (in the editor)
    6.  
    7. function Update(){
    8.  
    9.     if(livesCreatures == 0){ //the is no creature left; create a wave!
    10.  
    11.         if (waitToWave <= waveTime) {
    12.             waveTime = 0; //reset time
    13.             livesCreatures = totalCreatures; //fill the remaining creatures variable
    14.  
    15.             for (var i= 0; i < totalCreatures; i++) //Create a creature (as many to reach totalCreatures)
    16.                 Instantiate (yourCreature, Vector3(0,0,0), Quaternion.identity);
    17.  
    18.         }else {
    19.             waveTime += Time.deltaTime; //could fail xD!
    20.         }
    21.     }
    22. }
  8. Kokumo

    Kokumo

    New Member

    Joined:
    Jul 23, 2010
    Messages:
    390
    Badbii translated my bad c# typping into unity's java script... so now you can do the following, with a jscript class:

  9. StefanB95

    StefanB95

    New Member

    Joined:
    Mar 14, 2011
    Messages:
    97
    Thx alote guys it works perfectly i just need to make that it spawn more monster with a delay
    Last edited: Apr 9, 2011
  10. Kokumo

    Kokumo

    New Member

    Joined:
    Jul 23, 2010
    Messages:
    390
    More quantity of the same kind?... you just need to increase totalCreatures from 17 to the desired number.
    If you pretend more kind, you must make a major effort.

    About delay, you need to increase waitToWave value.
  11. StefanB95

    StefanB95

    New Member

    Joined:
    Mar 14, 2011
    Messages:
    97
    How to make to do a delay from spawning the current wave not in a sec but i mean it spawn one creep then the other after 0.5 sec and then the next till all 17 creeps are spawned and if all are dead do the next wave do you need to add a new var and a new part in the code or change something in this part of the code
    and about spawning other tipes of enemys i just made a new var with a nother transform and a nother instantiate after the last one something like this
    but i wanna to make it with the delay part of the code that that is the 5 monster other in the row that need to spawn so if you can help me just a litlle with it if no need than i will use the code that you will provide me thanks alote.
  12. badbii

    badbii

    New Member

    Joined:
    Dec 6, 2009
    Messages:
    83
    Use this script!
    Code (csharp):
    1. var spawnPoints : Transform[];  // Array of spawn points to be used.
    2. var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
    3. var amountEnemies = 20;  // Total number of enemies to spawn.
    4. var yieldTimeMin = 2;  // Minimum amount of time before spawning enemies randomly.
    5. var yieldTimeMax = 5;  // Don't exceed this amount of time between spawning enemies randomly.
    6.  
    7. function Start(){
    8.     Spawn();
    9. }
    10.  
    11. function Spawn(){
    12.    for (i=0; i<amountEnemies; i++){ // How many enemies to instantiate total
    13.       yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
    14.  
    15.       var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
    16.       var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.
    17.  
    18.       Instantiate(obj, pos.position, pos.rotation);
    19.    }
    20. }  
  13. Mono

    Mono

    New Member

    Joined:
    Jun 12, 2011
    Messages:
    4
    hey, i want to use this code, but i dont know where did i place it, in th empty game object? or in the enemy?, my problem is that when my enemy spawns it is supposed to follow me by an script, but it just moves randmly
  14. Mono

    Mono

    New Member

    Joined:
    Jun 12, 2011
    Messages:
    4
    where i must place it*
  15. kodagames

    kodagames

    Member

    Joined:
    Jul 8, 2009
    Messages:
    442
    you place it on an empty game object, if you want the enemy to follow thats another script you have to write thats placed on the enemy then make a prefab of the enemy which will follow you then you can add it the the empty game object (this script) which will spawn multiples in random locations.

    P.S. Thanks for the script guys I had one very similar which only spawned from one location so this is very sweet! Greatly appreciated.
  16. ghost012

    ghost012

    Member

    Joined:
    Jan 3, 2013
    Messages:
    8
    Oke,im gona ask you guys soemthing painfull.is it possible to instantiate a SpwanGroep? instead of a single spawn point?
    Need thsi so i can spawn mobs in formation(only the spawn,after spawn the formation isnt needed,so there is no need for constant formation)

    Logic wize: SpawnGroep1>Spawn1,Spawn2,Spawn3,Spawn4 ect.(all empty game objects)

    so i can spawn enemys in circle/cube/triangel formations around the player and attack the player.

    Im not a coder my self,you do not have to code it for me,but any leed to how tod o it would be fine.
  17. Tordur

    Tordur

    New Member

    Joined:
    Jul 28, 2012
    Messages:
    17
    What I did was to get different patterns was to use arrays.
    The way I did it was having an array for the creature pattern, so lets say I want like "Orc, Orc, Dragon, Dwarf", that becomes my pattern array.
    What I then did was to create a variable to store the amount of spawns in the pattern, so let's say I wanted to spawn nine creatures, It would take the pattern and spawn it like this "Orc, Orc, Dragon, Dwarf, Orc, Orc, Dragon, Dwarf, Orc" . And also if your pattern is longer than your spawn amount, it would only spawn the amount of creatures specified from the variable, while still following a part of the pattern.

    To do a formation, you could group the creatures into a gameobject, and just put the gameobject/prefab that holds the formation, directly into the spawn queue. Hope that helps :)
  18. Alter

    Alter

    New Member

    Joined:
    Jun 10, 2012
    Messages:
    9
    Is there a way to display how many enemies have been spawned?

    I've got:
    Code (csharp):
    1. GUI.Label (new Rect (10, 30, 1000, 20), "Enemies: " + amountEnemies.ToString ());
    But I want it to +1 each time an enemy appears and to also to display a timer between each wave. Cheers guys!