I’m trying to create an enemy spawn script that starts off slowly spawning enemies, and then slowly ramps up after each minute…as well as has a “large wave” of enemies at a certain time.
Everything runs smoothly for the first minute, and it feels like it transitions nicely into the second minute where the level increases, but for some reason there seems to be two seperate timers for the “SecondlevelSpawn”. And it will then just start spitting out 1 extra wave of Second level spawns.
I’ve been at this for about two days now, and I can’t find out why this is happening. Hopefully another set of eyes on this will help!
#pragma strict
static var playerDamage: float = 0; //records the damage the player has taken
var waveActive: boolean = true; //toggles for controlling the wave
var spawnEnemies: boolean = true; //
var bigSpawn: boolean = false;
var firstWaveActive: boolean = true;
var secondWaveActive: boolean = false;
var healthCount: int = 10; //players health count
var waveText: UILabel; //ui label
var healthText: UILabel;
var waveLevel: int = 0; //current wave we are on
var enemyPrefabs: GameObject[]; //array that contains all the enemy types
var enemyPrefabs02: GameObject[];
var alienSpawnPoints: Transform[]; //contains the spawn points
var respawnMinBase: float = 3.0; //used to ramp up the amount of spawns
var respawnMaxBase: float = 10.0; //
private var respawnMin: float = 3.0; //
private var respawnMax: float = 10.0; //
var difficulty: int = 0;
var seconds: int = 0;
var minutes: int = 0;
function Start ()
{
}
function Update ()
{
Clock ();
if (waveActive)
{
spawnEnemies = false;
FinishWave ();
}
if ( minutes >= 1 && firstWaveActive )
{
firstWaveActive = false;
secondWaveActive = true;
print ( "swapping over to second wave level");
waveActive = true;
}
if ( waveLevel == 09 && secondWaveActive )
{
secondWaveActive = false;
bigSpawn = true;
waveActive = true;
}
}
function SetNextWave () //prepares the values for the next wave (how many enemies will be spawn)
{
waveLevel ++; //ups the wave level
}
function StartNewWave ()
{
UpdateHUD (); //updates the gui
NewSpawnEnemy (); //spawns new enemy
waveActive = true; //sets the toggle to spawn enemies
spawnEnemies = true;
}
function UpdateHUD ()
{
waveText.text = "Wave: " +waveLevel; //updates the current waveLevel to the wave GUI
healthText.text = "Health " +healthCount; //updates the current healthLevel to the health GUI
}
function FinishWave ()
{
waveActive = false;
var i: int;
if ( minutes <= 1 && firstWaveActive && !secondWaveActive )
{
spawnEnemies = false;
i = Random.Range (20, 30);
print ("under 1 minute, waiting for "+i);
yield WaitForSeconds (i); //waits for the the intermission time to pass
SetNextWave (); //sets the next wave command
StartNewWave ();
}
if ( minutes >= 1 && !firstWaveActive && secondWaveActive)
{
spawnEnemies = false;
i = Random.Range (12, 18);
print ("over 1 minute, waiting for "+i);
yield WaitForSeconds (i); //waits for the the intermission time to pass
SetNextWave (); //sets the next wave command
StartNewWave ();
}
}
function NewSpawnEnemy ()
{
if ( minutes <= 1 && firstWaveActive && !bigSpawn )
{
FirstLevelSpawn ();
print ("first level spawn happening");
}
if ( minutes >= 1 && !bigSpawn && secondWaveActive && !firstWaveActive )
{
SecondLevelSpawn ();
}
if ( bigSpawn && !secondWaveActive )
{
BigSpawn ();
print ("big spawn happening");
}
}
function FirstLevelSpawn ()
{
var i = difficulty;
for (; i >= 0; i--)
{
var r = Random.Range (1, 2);
var enemyChoice = Random.Range (0, enemyPrefabs.Length);
var spawnChoice : int;
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
yield WaitForSeconds (r);
}
print ( "first level spawn finished");
}
function SecondLevelSpawn ()
{
var i = difficulty;
for (; i >= 0; i--)
{
var enemyChoice = Random.Range (0, enemyPrefabs02.Length);
var spawnChoice : int;
if ( enemyChoice == 1 )
{
var a = 1;
for (; a >= 0; a--)
{
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
}
}
if ( enemyChoice == 0 )
{
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
}
}
print ( "second level spawn finished" );
}
function BigSpawn ()
{
print ("Big Spawn!");
var i = 3;
for (; i >= 0; i--)
{
var enemyChoice = Random.Range (0, enemyPrefabs02.Length);
var spawnChoice : int;
if ( enemyChoice == 1 )
{
var a = 1;
for (; a >= 0; a--)
{
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
}
}
if ( enemyChoice == 0 )
{
spawnChoice = Random.Range (0, alienSpawnPoints.Length);
Instantiate (enemyPrefabs02[enemyChoice], alienSpawnPoints[spawnChoice].position, alienSpawnPoints[spawnChoice].rotation);
}
print ( "big spawn loop "+i);
}
bigSpawn = false;
secondWaveActive = true;
print ("big spawn finished");
}
function Clock ()
{
seconds = Time.time % 60;
minutes = Time.time / 60;
}