- Home /
Gradually decreasing float over time
Hello, I'm creating an enemy spawner system for my 2d platformer but can't get the spawn delay to gradually decrease over time. I've looked online but none of the answers seems to work. Here's the code:
public float delay;
public Transform[] spawnPoints;
public GameObject[] enemies;
int randomSpawnPoint, randomEnemy;
public bool spawnAllowed;
void Start () {
spawnAllowed = true;
InvokeRepeating("SpawnAnEnemy", 0f, delay);
}
void SpawnAnEnemy()
{
if (spawnAllowed)
{
randomSpawnPoint = Random.Range(0, spawnPoints.Length);
randomEnemy = Random.Range(0, enemies.Length);
Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
}
}
the delay is around 5 and I want it to slowly decrease to 1 after a certain amount of time. I tried using delay -= -0.001f; in an Update function but it didn't actually change the rate that the enemies were spawning
Any help would be very appreciated, thank you for your time.
Answer by xxmariofer · Jan 29, 2019 at 11:15 PM
Hello, have you test using coroutines? i find it more flexible and easier to use.
void Awake()
{
StartCoroutine(SpawnAnEnemy());
}
IEnumerator SpawnAnEnemy()
{
while (spawnAllowed)
{
randomSpawnPoint = Random.Range(0, spawnPoints.Length);
randomEnemy = Random.Range(0, enemies.Length);
Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
yield return new WaitForSeconds(delay);
delay -= delayReduction;//0.5 for example be carefull not make it less than 0
if(delay <= 0) spawnAllowed = false;
}
}
This fixed the problem! The coroutine was definitely easier than what I was trying so ill try and use more of them in the future if I can. Thanks for your help :)
By any chance would you know how to put a limit on this? so the delay doesn't go below 1 second?
just an if before the delay reduction and when it gets to 0 change spawnAllowed to false, that will make the while to finish.
so something like
if( delay == 0) { spawnAllowed = false; }
??? sorry I'm not the best at coding
Note: in your current setup whenever you set "spawnAllowed" to false your application (and editor) will hang since you're trapped inside an infinite loop without yield. When you create an infinite loop in a coroutine (which is perfectly fine) you have to ensure that you always have a yield statement that is executing.
Sometimes it makes more sense to write it like this:
while (true)
{
while (!spawnAllowed)
yield return null;
randomSpawnPoint = Random.Range(0, spawnPoints.Length);
randomEnemy = Random.Range(0, enemies.Length);
Instantiate(enemies[randomEnemy], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
yield return new WaitForSeconds(delay);
delay -= delayReduction;//0.5 for example be carefull not make it less than 0
}
That way you have a better overview where and when your yields fire.
Thats true i didnt take that into account, i will edit the answer thanks :)
@$$anonymous$$iniGameDev2208 Take @Bunny83 code into account or the new edited answer, since the previous one would give you problems.
What does the yield return null do? Does it stop the rest of the coroutine if the spawnAllowed is false?
Your answer
Follow this Question
Related Questions
Make enemy wait before attacking player 2 Answers
AudioSource playing with delay for 1 step 0 Answers
How do I put a delay in this? 2 Answers
Touch input reporting 0 delta for first several Updates on iOS, 2 Answers
Invalid floats in PlayerPrefs 3 Answers