- Home /
How to increase the spawn rate of an object over time?
Hey everyone, I'm new to coding. It seems like people are very nice and helpful on here, so I thought I'd ask a question I've been stuck on for a long time now. I'm currently making a 2D game where balls are dropping from the top of the screen to the bottom of the screen. Your goal is to pick them up, and if you let one fall on the ground, you lose. The spawn rate is set to 2 sec. I want this increased by 0.1 sec every 30 seconds (2 sec, 1.9 sec, 1.8 sec, etc.). I feel like I'm close, but I can't seem to get it to work. Here is the code I'm working on. Tell me, how can I make this happen? Thank you in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnBallsNew : MonoBehaviour
{
public GameObject fallingObject;
float delayAndSpawnRate = 2;
float timeUntilSpawnRateIncrease = 30;
void Start()
{
InvokeRepeating("SpawnObject", delayAndSpawnRate, delayAndSpawnRate);
StartCoroutine(ScheduleIncreases());
}
void SpawnObject()
{
float x = Random.Range(-11, 11);
Instantiate(fallingObject, new Vector2(x, 7), Quaternion.identity);
}
IEnumerator ScheduleIncreases()
{
yield return new WaitForSeconds(timeUntilSpawnRateIncrease);
IncreaseSpawnRate();
StartCoroutine(ScheduleIncreases());
}
void IncreaseSpawnRate()
{
if (delayAndSpawnRate > 1)
{
delayAndSpawnRate -= 0.1f;
}
}
}
Answer by Hellium · May 09, 2019 at 02:08 PM
public class SpawnBallsNew : MonoBehaviour
{
public GameObject fallingObject;
float delayAndSpawnRate = 2;
float timeUntilSpawnRateIncrease = 30;
void Start()
{
StartCoroutine(SpawnObject(delayAndSpawnRate));
}
IEnumerator SpawnObject( float firstDelay )
{
float spawnRateCountdown = timeUntilSpawnRateIncrease ;
float spawnCountdown = firstDelay ;
while( true )
{
yield return null ;
spawnRateCountdown -= Time.deltaTime;
spawnCountdown -= Time.deltaTime;
// Should a new object be spawned?
if( spawnCountdown < 0 )
{
spawnCountdown += delayAndSpawnRate;
Instantiate(fallingObject, new Vector2(Random.Range(-11, 11), 7), Quaternion.identity);
}
// Should the spawn rate increase?
if( spawnRateCountdown < 0 && delayAndSpawnRate > 1 )
{
spawnRateCountdown += timeUntilSpawnRateIncrease;
delayAndSpawnRate -= 0.1f;
}
}
}
}
Thank you for such a quick response, I really appreciate it. I did not manage to get it to work however, it got a bit confusing when you set variables I've already defined to mean something new, and I also didn't understand whether or not what you answered was the complete script or something I should add into $$anonymous$$e (I assumed the complete script though). I edited the question text, as I might have been unclear earlier, so just in case, take a look again if you can. What should I do? @Hellium
The script is meant to replace yours.
What does not work with it?
spawnRateCountdown
is the remaining time before the delayAndSpawnRate
is decreased.
spawnCountdown
is the remaining time before a new falling object is instantiated.
When those countdown reach 0, they are reset to their original values (`timeUntilSpawnRateIncrease` and delayAndSpawnRate
respectively)
Lol, not sure what I was doing the first time around while testing, as I tested it again and it worked perfectly. I guess I just got a bit confused from the code, since I'm new to this and, yeah, anyways, it worked perfectly, so thank you very much for helping me so quickly, have a great rest of your day/night.
Answer by ajmracer032706 · Sep 29, 2020 at 10:58 AM
@Hellium you may not see this but it is worth a shot anyway. I am currently using your code and I have the Delay and Spawn Rate set to 20 along with the timeUntilSpawnRateIncrease set to 1. For some reason the script stops working when the Delay and Spawn Rate reaches 18.09999. Any suggestions?,I don't know if anyone will see this but when I implement this code it stops working after some of the countdown. If I start it at 20 Delay and Spawn Rate with timeUntilSpawnRateIncrease of one it ends at 18.09999999. If anyone sees this that may have a fix it is very apprecitated!