- Home /
Spawing Enemies works but works to good
The problem i am having is that i only want it to spawn a certain amount of enemies if the amount is met then stop spawning the enemies. But if the enemy count is less than the max Amount start spawning again. Here is my code it works really good but the above is a problem any help would be amazing thanks.
PS. I don't know why this code 101010 button is not working correctly
var AISpawn : Transform[]; var allEnemies : GameObject[]; static var enemiesToSpawn : int; static var currentEnemies : int; var maxEnemiesInt : int; var maxEnemies : boolean = false; var wave1 : boolean = true;
function Awake()
{
if(wave1 == true)
{
Wave1();
}
}
function Update()
{
if(currentEnemies >= maxEnemiesInt)
{
maxEnemies = true;
}
}
function Wave1()
{
if(maxEnemies == false)
{
enemiesToSpawn = 10;
for(i=0; i < enemiesToSpawn; i++)
{
var obj : GameObject = allEnemies[Random.Range(0, allEnemies.length-1)]; // Randomize the different enemies to instantiate.
var pos: Transform = AISpawn[Random.Range(0, AISpawn.length-1)]; // Randomize the spawnPoints to instantiate enemy at next.
Instantiate(obj, pos.position, pos.rotation);
yield WaitForSeconds(2);
currentEnemies +=1;
}
}
}
Hi Aldonletto one quick Question how would i make this create more enemies if the max enemies has been decreased. I thought maybe it would reset to true if maxenemies was decreased i would really appreciate your help
Answer by aldonaletto · Sep 23, 2011 at 01:15 AM
I would use another approach: define minEnemies and maxEnemies, and let the coroutine start spawning enemies until maxEnemies is reached. It then stops, and only restart spawning when the number of enemies fall below minEnemies. For this to work, every enemy must decrement currentEnemies before going to hell, or the coroutine will never know when to restart. You can use Start to do the whole thing, since it's allowed to be a coroutine. The yield WaitForSeconds(2) instruction at the end of the loop gives a 2 seconds pace: each 2 seconds it checks the enemy number and spawns one of them if necessary. Finally: you should use Random.Range(0, array.length), since this function goes to max-1 in the integer version (length-1 makes the last array element never be used)
var AISpawn : Transform[]; var allEnemies : GameObject[]; static var currentEnemies : int = 0; var maxEnemies : int = 15; var minEnemies : int = 10; var spawn = false;
function Start(){
while (true){
if (currentEnemies < minEnemies)
spawn = true; // if minEnemies reached start spawning
if (currentEnemies >= maxEnemies)
spawn = false; // if maxEnemies reached stop spawning
if (spawn){ // if spawning enabled create new enemy
// Randomize the different enemies to instantiate.
var obj : GameObject = allEnemies[Random.Range(0, allEnemies.length)];
// Randomize the spawnPoints to instantiate enemy at next.
var pos: Transform = AISpawn[Random.Range(0, AISpawn.length)];
Instantiate(obj, pos.position, pos.rotation);
currentEnemies +=1;
}
yield WaitForSeconds(2); // free Unity for 2 seconds each loop anyway
}
}
I am going to kiss you if i ever meet you aldonaletto
Thank you so much
There is something i would like to ask you if i may?Can i create an array of functions so that when the player is on a certain wave/Round. It can just call that function Increase the round number and decrease the weapons damage. I know how to most of that but it is just i do not want to have to create 1000's of rounds just one that i can all and increase-decrease things?
I think you can declare a function array as var funcs: function[];, initialize and populate it by script, then use it as funcs[n]();
I didn't test this yet - I've just read something about this in an answer from @mohanrao164, which linked to this wiki about delegate: http://www.unifycommunity.com/wiki/index.php?title=$$anonymous$$ain$$anonymous$$enu
Answer by TheEmeralDreamer · Sep 23, 2011 at 01:02 AM
It seems you need to place a while loop like so inside of the wave function to hold all your other code.
while(playerisinthearea or something like that.) { if(#ofenemiesislessthaniwant) { //create enemies } else { //return to do nothing }
}
Your answer
Follow this Question
Related Questions
Creating a looping spawn with an Array 0 Answers
How to create random spawn for an object? (C#) 0 Answers
Increase number of enemies spawned per level 0 Answers
How to spawn enemy? (Enemies) 1 Answer