- Home /
Increasing Spawn Rate as Game Goes On
Hi. In my game I have a player climbing up a wall and objects fall on the player. (Credits to @static_cast). I was wondering how I can increase the spawn rate of the object every 30 seconds. In other word to make more objects fall as the game goes on. How should I do this?
@$$anonymous$$rSoad I like that idea but I only would like to increase the spawn rate. I am a beginner intermediate coder so I am not so sure you to make a difficulty script. Is there anyway I can just increase the spawn rate every 30 seconds?
@$$anonymous$$rSoad I have two questions. One how do I set a limit. And two I'm not sure what the last sentence of your comment really means. How do I restart my spawn function?
Answer by MrSoad · Dec 08, 2014 at 01:18 AM
Hi again, take a look at this code. I have not ran this so maybe there is a typo somewhere :
private var fSpawn_Speed : float;
function Start() {
//Set initial Spawn Speed
fSpawn_Speed = 10f;
//Start Spawning.
InvokeRepeating("subSpawn_Object", 0, fSpawn_Speed);
//Start the Spawn speed adjust in 30 seconds.
InvokeRepeating("subIncrease_Spawn_Speed", 30, 30);
}
function subIncrease_Spawn_Speed() {
var fSpeed_Increase : float = 1f;
//Cancel the current subSpawn_Object Invoke.
CancelInvoke("subSpawn_Object");
//This will limit the spawn speed to a min of 1.
if ((fSpawn_Speed - fSpeed_Increase) < 1) {
fSpawn_Speed = 1f;
} else {
fSpawn_Speed = fSpawn_Speed - fSpeed_Increase;
}
//Restart subSpawn_Object with new repeat time.
//You may want to adjust for the time since the last spawn
//by setting up a time since last spawn var and using that
//calculation rather than 0.
InvokeRepeating("subSpawn_Object", 0, fSpawn_Speed);
}
function subSpawn_Object() {
//Your Spawn code.
}
Couldn't you just use Time.time and nextSpawn to change the spawn times?
@$$anonymous$$rSoad This is great except the transition is not smooth. It starts at one and then after 45 seconds (Which is what I want it to be ins$$anonymous$$d of 30) it goes to 0.5 and then after another 45 seconds it goes to 0.3. Is there anyway to have a smooth transition, so that it goes like 1 down to 0.9 to 0.8 to 0.7... etc.?
Did you try my solution?
It's possibly not smooth because you need to change the fSpeed_Increase variable. ;)
Thank you so much @static_cast and @$$anonymous$$rSoad! You guys have helped me so much! @static_cast I tried to try your solution but I didn't know where to put my instantiate code in. Anyways I will tag you guys if I need any more help with this topic.
You just replace your code with it.
Basically, delete your original Update() and replace it with $$anonymous$$e. ;)
Answer by static_cast · Dec 08, 2014 at 01:29 AM
Here's what you could do:
private float nextDrop = 0f; //The time for the next item to spawn
private float dropInterval = 10f; //The interval between spawned items
private float changeInterval = 30f; //The interval between changing the interval between spanwed times. :P
void Update()
{
if(Time.time >= nextDrop) //If ready to spawn
{
SpawnObject();
nextDrop += dropInterval; //Set next spawn time
if(Time.time >= changeInterval) //If ready to change spawn interval
{
if(dropInterval > 1f) //Change spawn interval to 3/4ths what it was
dropInterval *= 0.75f;
else //Make sure dropInterval stays above 1.
dropInterval = 1f;
}
}
}
To me, working code is not the same as organized code. You can put code into 10 nested coroutines, or you could put it where it counts.
Why not put it in Update()? From what I've seen of this guy's code, this is his spawning script. Here's what http://forum.unity3d.com/threads/invokerepeating-vs-update-battle-of-efficiency.214901/ has to say about it: "That isn't to say, however, that there isn't a lot more overhead required to call your "Invoke" method through InvokeRepeating. It's doing this calling through reflection, which is slow. (see: $$anonymous$$ethodInfo.Invoke $$anonymous$$ethod)"
@static_cast I like your idea but where would I put my spawn code?
True the big long update, but I don't see what could possible be wrong with the script. It works, it's efficient, and it's the purpose of said script.
I wouldn't put walking scripts in a script used to spawn objects, would I?
@$$anonymous$$rSoad I code in C# so I translated your code into C# and the only error I'm getting is that the fSpeed_Increase variable was never declared.
Do you know how to fix that Vinnya124 ?
Just incase you don't replace :
var fSpeed_Increase : float = 1f;
with :
float fSpeed_Increase = 1f;
Answer by martin101 · Dec 12, 2014 at 09:54 AM
Download this free asset in which there are 4 games. One of the games uses Spawn function which is what you need.
C# game examples by M2H
https://www.assetstore.unity3d.com/#!/content/116 Play the Egg bucket Game ( thats what you need )
Your answer
Follow this Question
Related Questions
Updating then calling gameObject from main thread. error 0 Answers
InvokeRepeating 1 Answer
If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Game freezes everything when calling WaitForSeconds 1 Answer