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
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): int totalCreatures = 17; //I set 17 because i like 17 int livesCreatures = 0; //remaining creatures; this number will vary through the game float waitToWave = 10; //we will wait 10 second for the next wave, after the las creature float waveTime; //remaining time to reach waitToWave; Transform yourCreature; //this must be public to set your creature prefab (in the editor) function Update() { if (livesCreatures == 0) { //the is no creature left; create a wave! if (waitToWave <= waveTime) { waveTime = 0; //reset time livesCreatures = totalCreatures; //fill the remaining creatures variable for (int i = 0; i < totalCreatures; i++) //Create a creature (as many to reach totalCreatures) Instantiate (yourCreature, Vector3(0,0,0), Quaternion.identity); }else { waveTime += Time.DeltaTime; //could fail xD! } } } 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...
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
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.
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.
script without any mistakes in coding: Code (csharp): var totalCreatures : int = 17; //I set 17 because i like 17 var livesCreatures : int = 0; //remaining creatures; this number will vary through the game var waitToWave : float = 10; //we will wait 10 second for the next wave, after the las creature var waveTime: float; //remaining time to reach waitToWave; var yourCreature :Transform; //this must be public to set your creature prefab (in the editor) function Update(){ if(livesCreatures == 0){ //the is no creature left; create a wave! if (waitToWave <= waveTime) { waveTime = 0; //reset time livesCreatures = totalCreatures; //fill the remaining creatures variable for (var i= 0; i < totalCreatures; i++) //Create a creature (as many to reach totalCreatures) Instantiate (yourCreature, Vector3(0,0,0), Quaternion.identity); }else { waveTime += Time.deltaTime; //could fail xD! } } }
Badbii translated my bad c# typping into unity's java script... so now you can do the following, with a jscript class:
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.
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.
Use this script! Code (csharp): var spawnPoints : Transform[]; // Array of spawn points to be used. var enemyPrefabs : GameObject[]; // Array of different Enemies that are used. var amountEnemies = 20; // Total number of enemies to spawn. var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly. var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly. function Start(){ Spawn(); } function Spawn(){ for (i=0; i<amountEnemies; i++){ // How many enemies to instantiate total yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated. var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate. var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next. Instantiate(obj, pos.position, pos.rotation); } }
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
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.
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.
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
Is there a way to display how many enemies have been spawned? I've got: Code (csharp): 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!