Unity Community


Results 1 to 18 of 18

  1. Posts
    97

    TD Wave spawning

    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. Location
    Argentina
    Posts
    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:  
    1. int totalCreatures = 17; //I set 17 because i like 17
    2. int livesCreatures = 0; //remaining creatures; this number will vary through the game
    3. float waitToWave = 10; //we will wait 10 second for the next wave, after the las creature
    4. float waveTime; //remaining time to reach waitToWave;
    5. Transform yourCreature; //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 (int 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. }

    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 by Kokumo; 04-08-2011 at 01:20 PM. Reason: mispelled variables
    I'm a spanish native speaker, so excuse me if i make a mistake.

    You don't know nothing about scripting? Take a look at this [link]


  3. Posts
    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. Location
    Argentina
    Posts
    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 by Kokumo; 04-08-2011 at 01:35 PM.
    I'm a spanish native speaker, so excuse me if i make a mistake.

    You don't know nothing about scripting? Take a look at this [link]


  5. Posts
    97
    I got a error on the script or don;t now it says
    A namespace can only contain types and namespace declaration


  6. Location
    Argentina
    Posts
    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.
    I'm a spanish native speaker, so excuse me if i make a mistake.

    You don't know nothing about scripting? Take a look at this [link]


  7. Posts
    83
    Quote Originally Posted by StefanB95 View Post
    I got a error on the script or don;t now it says
    script without any mistakes in coding:
    Code:  
    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. Location
    Argentina
    Posts
    390
    Badbii translated my bad c# typping into unity's java script... so now you can do the following, with a jscript class:

    Quote Originally Posted by Kokumo View Post
    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.
    I'm a spanish native speaker, so excuse me if i make a mistake.

    You don't know nothing about scripting? Take a look at this [link]


  9. Posts
    97
    Thx alote guys it works perfectly i just need to make that it spawn more monster with a delay
    Last edited by StefanB95; 04-09-2011 at 04:08 AM.


  10. Location
    Argentina
    Posts
    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.
    I'm a spanish native speaker, so excuse me if i make a mistake.

    You don't know nothing about scripting? Take a look at this [link]


  11. Posts
    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
    if (waitToWave <= waveTime) {
    waveTime = 0; //reset time
    livesCreatures = totalCreatures; //fill the remaining creatures variable
    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
    for (var i= 0; i < totalCreatures; i++) //Create a creature (as many to reach totalCreatures)
    Instantiate (yourCreature, transform.position, Quaternion.identity);
    Instantiate (yourCreature2, transform.position, Quaternion.identity);
    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. Posts
    83
    Use this script!
    Code:  
    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. Posts
    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. Posts
    4
    where i must place it*


  15. Posts
    435
    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.
    Black Jack Video Tutorial with 11 hours of Video Click Here
    .............................. ............................... .........................


  16. Posts
    6
    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. Posts
    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
    I7 3770K - 8GB Ram - GTX 560TI
    Game Developer at Narauk


  18. Posts
    9
    Is there a way to display how many enemies have been spawned?

    I've got:
    Code:  
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •