- Home /
How increase and decrease and reset spawn rate over time?
How I Can Increase And Decrease Spawn Rate Every Time I Want? i can Increase Not Decrease Or Reset
public float nextDrop = 0f;
public float dropInterval = 2f;
public float changeInterval = 5f;
float delay = 3f;
int minX = -6;
int maxX = 6;
int indexx;
public Vector3 spawnvalue;
public GameObject[] targets;
private void Update()
{
if(Time.time >= nextDrop)
{
Spawn();
nextDrop += dropInterval;
if(Time.timeSinceLevelLoad >= changeInterval)
{
if(dropInterval > 0.3f)
{
dropInterval *= 0.92f;
}
}
if(Time.timeSinceLevelLoad >= 50f)
{
if(dropInterval < 0.3f)
{
dropInterval = 2f;
}
}
if(Time.timeSinceLevelLoad >= 60f)
{
if(dropInterval > 0.3f)
{
dropInterval *= 0.92f;
}
}
}
}
void Spawn()
{
GameObject spawn = Pool.Instance.GetPool();
spawn.transform.position = new Vector3 (Random.Range (minX, maxX), spawnvalue.y,
targets[indexx].transform.position.z + 50 );
spawn.SetActive(true);
}
Answer by tormentoarmagedoom · Mar 08, 2020 at 11:50 AM
Hello!
Thanks for posting again, now is much easy to read your post!.
For your code, i see you spawn using Spawn()
wich is called in the Update
.
But i dont understand why you use Time.time
to compare with nextDrop
Time.time is the time in seconds since the start of the game.
If you pretend to do something every X seconds, you have differnt options, but thisone you use is...mm strange and difficult to operate with.
The best solution is using IEnumeratorts
(Corutines), really, they are very easy to use (also for beginers) and will give you control of timing functions. You can hold a function before continue or before commence again. I recommend you 100% to spend next 2 hours learning about corutines, there are several manuals andyoutube tutorials.
Another way can be creating a timer, similar of what you are doing in your script. But never using Time.time, instead you should have a float varaibe that increase with Time.deltaTime and when your variable reach some Maxvalue, you spawn again. And changing thes MAxValue will make change time between spawns.
There are so many posts about Repeat a function over time, about timers, about spawning.
You should take your time, reading Unity Answers and watching tutoprials. Speed is not a good friend when learning scripting.
Good luck!
thanks for reply, i understand use coroutine i s so much better but i don't know how do every x seconds with coroutine and we have no tutorial about decrease spawn rate. you think i should do something like this?
void update() { spawntime += Time.deltaTime; }
IEnumerator Spawn() { while (true) { if(spawntime== 3f) { //startspawn } if(spawntime== 10f) { //increasespawn } if(spawntime== 30f) { //decreasespawn } } }
Hello again.
We are developers, we talk about functions and variables.
" no tutorial about decrease spawn rate" means nothign while developing.
you want a tutorial to repeat a function every xome seconds, and change this seconds if needed, dont care if respawing, damaging, posionig... we are not the players.
You need to use corutine like this:
float SecondToWait = 5f;
void Start()
{
StartCorutine(Respawn())
}
IEnumerator Respawn()
{
//Respawn code
yield return New WaitForSeconds(SecondToWait);
StartCorutine(Respawn())
}
If you change the SecondToWait value, the fucntion will wait different seconds time.
thanks i use coroutine before and don't work for me but i try your method
Your answer
Follow this Question
Related Questions
Increase/Decrease Over Time, Not Frames 1 Answer
How Increase And Decrease And Reset Spawn Rate Over 0 Answers
How To Add A Simple Speed Boost On Collision With A Specific Object With A Character Controller 0 Answers
slowly decrease value when in trigger 1 Answer
Problems with Spawner and timer. 1 Answer