- Home /
How would I create a script that spawns objects more frequently as time goes on?
For my game, I have an enemy that works perfectly. Now how would I make a mode, in which the enemy spawns maybe 1 for the first minute, then 20 at the twentieth minute. So that every minute, the spawn goes up 1 or 2. I have no idea where to start with a script like this, so any help would be appreciated.
I assume it would be with Instantiate and Invoke, I don't think this is the answer but here is an Idea I have: Instead of normally spawning, like: InvokeRepeating("Spawnenemy", 5) would InvokeRepeting("Spawnenemy", Variablethatgoesup) work?
You are on the right track. You also need to know how to stop InvokeRepeating before re-invoke the same function.
Thanks, everyone's answered worked, just picked the one I used. Thanks again,
Answer by sparkzbarca · Mar 17, 2013 at 07:18 AM
lots of ways to do this of course but if you want to link spawn rate to time
assuming you want the spawn to be at the same moment (that is spawn 20 in 1 frame) then you can do
every minute spawn x where x equals
spawnrate = Time/60 (time is a built in object, it returns the time in seconds since game start)
if(Time/60 == 0)
for(int x = 0; x < spawnrate; x++)
spawn();
if you'd like you can just spawn 1 every x seconds but give the rate a curve so you spawn more often but you spawn only 1 each time something like
SpawnRate = 60; // we'll start out spawning once a minute
if (Time/SpawnRateInSeconds == 0)
spawn();
now you write a script to reduce spawnrate by so much every so long that'd look something like
if(Time/60 == 0) // every minute were going to modify spawn rate
SpawnRate -= 5;
so in this case we'll spawn 1 the first minute and by minute 6 we'll be spawning 2 every minute.
The main issue with this method is it's going to get basically impossible because the growth is goign to ramp hard
that is to say when the spawnrate is 60 seconds taking 5 seconds off wont matter much.
but later on when its 10 seconds taking 5 seconds off will basically double the spawn rate making it ramp up hugely. Your basically looking at exponential growth here. you'll want to make sure also that you down let spawn rate drop below 0 (or you'll have bizzare behavior). I put that in in case you acutally wanted this kind of large exponential curve.
You could have a non curved growth as well you could do that by using division by the rate soemthing like
if(Time/60) //again we'll assum we want to change once a minute
spawnrateinseconds = spawnrateinseconds/4
or generaly
.... = spawnrate/x
where x is the rate
so if its 4 we'll make it so every minute it spawns 25% faster/ but it'll be a constant slope where as we get to lower spawn times we'll also take less off the timer meaning you wont get these large spikes at the end.
MARK AS ANSWERED
Answer by whydoidoit · Mar 17, 2013 at 07:35 AM
A very useful built in feature of Unity is the AnimationCurve which despite it's name - you can use for things apart from animation!
Add a variable of type AnimationCurve to your script, then use the animation editor to create a curve which relates time on the x axis to some other value on the Y axis - in your case the delay between spawning enemies. In the inspector it might look a bit like this:
The animation curve editor is a bit fiddly - but once you get the hang of it its fine.
Then what you need is a coroutine which uses a value from the curve:
In C#
public AnimationCurve curve;
IEnumerator SpawnEnemies()
{
while(true)
{
yield return new WaitForSeconds(curve.Evaluate(Time.timeSinceLevelLoaded));
//Spawn your enemy
}
}
Answer by robertbu · Mar 17, 2013 at 07:44 AM
I looked at a few curves, but nothing seemed to fit your specification. So here is a more literal implementation (untested):
var startTime : float;
var nextTime : float;
function Start () {
startTime = Time.time;
nextTime = Time.time;
}
function Update () {
if (Time.time > nextTime)
{
var minutes : float = Mathf.Floor((Time.time - startTime) / 60.0);
nextTime = Time.time + 60.001 / (minutes + 1.0);
// Spawning code goes right here
}
}
It will spawn 1 the first minute, 2 the second minute, 3 the third...