So What my idea was is that a spawner would spawn 1 enemy if the time was more than one second and that the boolean was true. The problem is that eventhough it’s supposed to become false to prevent mass spawning it still spawns a ton more than just 1 enemy
var SpawnTime;
var EasyEnemy : GameObject;
var SpawnRate = 15;
var SRdecrease = .025f;
var EnemySpawnCoolDown = true;
var time : float;
function Start () {
}
function Update () {
SpawnRate -= (Time.deltaTime * SRdecrease);
SpawnEasy();
}
function SpawnEasy() {
SpawnTime = Time.time;
if( SpawnTime >= 1 && EnemySpawnCoolDown == true){
EnemySpawnCoolDown = false;
Instantiate( EasyEnemy, transform.position, transform.rotation );
Debug.Log("EnemyHasSpawned!");
yield WaitForSeconds(SpawnRate);
EnemySpawnCoolDown = true;
}
}
where does the code go wrong?
here try this this way is much better
// Add this script to a Parent GameObject of the spawnPoints.
// Note: enemyPrefab will have an AI script attached which will already Tag the Player object
// so it won't be needed here.
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);
}
}