Hi, I’ve been trying to search different ways to handle this enemy spawnrates. Currently im using InvokeRepeating but saw that it can’t handle a changing repeat value. I’ve also tried WaitForSeconds but couldn’t understand how that should work or is that even the right thing to do. So what is the answer for this problem?
Example: I want to spawn enemy object every 1 second, but every 5 seconds that spawnrate is 0.2 seconds smaller.
#pragma strict
// Variable to store the enemy prefab
public var enemy : GameObject;
var enemySpawnRate = 1;
function Start() {
// Call the 'addEnemy' function every 'enemySpawnRate' seconds
InvokeRepeating("addEnemy", 1, enemySpawnRate);
InvokeRepeating("increaseSpawnRate", 1, 5);
}
// New function to spawn an enemy
function addEnemy() {
// Variables to store the X position of the spawn object
var x1 = transform.position.x - renderer.bounds.size.x/2;
var x2 = transform.position.x + renderer.bounds.size.x/2;
// Randomly pick a point within the spawn object
var spawnPoint = new Vector3(Random.Range(x1, x2), transform.position.y,8);
// Create an enemy at the 'spawnPoint' position
Instantiate(enemy, spawnPoint, Quaternion.identity);
}
function increaseSpawnRate(){
GameMaster.enemySpawnRate -= 0.2 ;
}