- Home /
Spawning different enemies for different levels
Hello everyone. I am making a 2D shooter and I want to introduce new enemies after certain levels are reached. I created a GameObject[] called enemies. I added 2 enemy prefabs to the Array. It's working except that when level 6 is reached, I want to ONLY start spawning enemy[1] not enemy[0] but it keeps spawning both. Can anyone see what I'm doing wrong?
function LevelEnd(){
EnemySpeed += -.1;
if(level <=5){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[0], Vector3(posX, 0.1, 3.545458),transform.rotation);
}
}
if(level >= 6 && level < 10){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
}
}
if(level >= 10 && level < 15){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
}
}
}
EDIT GameController.js function Update () { if(!playerspawning){ timer += Time.deltaTime; } //when timer reaches 2 seconds, call Spawn function if(timer >= 2){ Spawn(); }
if(level == 0 ||cash == level * 200){
level++;
LevelEnd();
}
}
function LevelEnd(){
//I don't know about the rest of your code, but you have to do
//something like this:
totalEnemies = 0;
EnemyMovement.EnemySpeed += -.1;
if(level <=5){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[0], Vector3(posX, 0.1, 3.545458),transform.rotation);
// Update your totalEnemies variable
totalEnemies++;
}
}
if(level >= 6 && level < 10){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
// Update your totalEnemies variable
totalEnemies++;
}
}
if(level >= 10 && level < 15){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
// Update your totalEnemies variable
totalEnemies++;
}
}
}
EnemyMovement.js var enemySpeed:float; static var CoastguardSpeed:float;
function Awake(){
enemySpeed = EnemySpeed;
}
function Update () {
transform.Translate(0,0,enemySpeed * Time.deltaTime);
if(transform.position.z < -2.04){
GameController.totalEnemies -= 1;
Destroy(gameObject);
}
}
Answer by bompi88 · May 08, 2012 at 09:55 PM
You have to update your totalEnemies variable when you instantiates a new enemy. If you don't do this, it keeps looping in the while loop.
function LevelEnd(){
//I don't know about the rest of your code, but you have to do
//something like this:
totalEnemies = 0;
EnemySpeed += -.1;
if(level <=5){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[0], Vector3(posX, 0.1, 3.545458),transform.rotation);
// Update your totalEnemies variable
totalEnemies++;
}
}
if(level >= 6 && level < 10){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
// Update your totalEnemies variable
totalEnemies++;
}
}
if(level >= 10 && level < 15){
while(totalEnemies < level){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
// Update your totalEnemies variable
totalEnemies++;
}
}
}
I've edited the code to fit your needs:
var isSpawningEnemies : boolean = true;
function LevelEnd(){
// what's the intention of this?
EnemySpeed += -.1;
while (isSpawningEnemies) {
if(level <=5){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[0], Vector3(posX, 0.1, 3.545458),transform.rotation);
}
if(level >= 6 && level < 10){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
}
if(level >= 10 && level < 15){
posX = Random.Range(-4.213592,4.336067);
yield WaitForSeconds(3);
Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
}
}
}
for not to spawn enemies, just:
isSpawningEnemies = false;
Didn't seem to have any effect at all, but thanks for re$$anonymous$$ding me to add to totalEnemies but after I do this the enemies stop spawning once totalEnemies = level. What's supposed to happen is Waves of enemies will continuously spawn and move toward the player. When they move off screen they are destroyed. The player has to shoot or avoid the enemies while collecting randomly spawning cash. I tried adding GameController.totalEnemies--; to the enemy movement script right before they're destroyed but this didn't do anything. I'm making an edit with more of my code above.
The first while loop loops forever! Just as Bjoern said.
Try some debug logs and you will see.
Ahh yes I see that now. Will work on it tmw and post my solution.
You can try my updated code, and see if it behaves properly. Haven't tested it yet.
That worked great! Thanks. I also see where I went wrong. Thank you very much for explaining it and the example!
BTW EnemySpeed += -.1; increases the speed at which the enemy moves down the screen.