Hey everybody, I'm almost brand new at this. My issue is, once the first wave of enemies are defeated, the second wave never begins. I'm using the first tutorial from the Unity Game Development Blueprints e-book. It's a top-down space shooter, real basic. Here's what I have (sorry if all the comments are annoying, but this is my first few days of programming ever):
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
// Our enemy to spawn
public Transform enemy;
// We want to delay our code at certain times
public float timeBeforeSpawning = 1.5f;
public float timeBetweenEnemies = .25f;
public float timeBeforeWaves = 2.0f;
public int enemiesPerWave = 10;
private int currentNumberOfEnemies = 0;
// Coroutine used to spawn enemies
IEnumerator SpawnEnemies()
{
// Give the player time before we start the game
yield return new WaitForSeconds(timeBeforeSpawning);
// After timeBeforeSpawning has elapsed, we will enter this loop
while(true)
{
// Don't spawn anything new until all of the previous
// wave's enemies are dead
if(currentNumberOfEnemies <= 0)
{
float randDirection;
float randDistance;
//Spawn 10 enemies in a random position
for (int i = 0; i < enemiesPerWave; i++)
{
// We want the enemies to be off screen
//(Random.Range gives us a number between the
//first and second parameter)
randDistance = Random.Range(10, 25);
// Enemies can come from any direction
randDirection = Random.Range(0, 360);
// Using the distance and direction we set the position
float posX = this.transform.position.x +
(Mathf.Cos((randDirection) * Mathf.Deg2Rad) *
randDistance);
float posY = this.transform.position.y +
(Mathf.Sin((randDirection) * Mathf.Deg2Rad) *
randDistance);
// Spawn the enemy and increment the number of enemies spawned
//(Instantiate makes a clone of the first parameter
//and places it at the second with a rotation of
//the third.)
Instantiate(enemy, new Vector3 (posX, posY, 0),
this.transform.rotation);
currentNumberOfEnemies++;
yield return new WaitForSeconds(timeBetweenEnemies);
}
}
// How much time to wait before checking if we need to
// spawn another wave
yield return new WaitForSeconds(timeBeforeWaves);
}
}
// Allows classes outside of GameController to say when we killed an enemy.
public void KilledEnemy()
{
currentNumberOfEnemies--;
}
// Use this for initialization
void Start ()
{
StartCoroutine(SpawnEnemies());
}
// Update is called once per frame
void Update () {
}
}
↧