- Home /
How can i make a Cube Spawner spawn cubes faster and faster?
Hello. I have a game right now where my character is steering through cubes. Right now my cubes spawn 100 units in front of the player, each 0.75 seconds. I want it to spawn cubes faster and faster all the way down to 0.25, and then that should be the fastest. Here is some of the code i have now:
void Update () {
if ( spawnTime < Time.time ) {
CreateCube ( spawnDistance + character.position.z );
spawnTime += delayBetweenSpawns;
}
}
}
Answer by Lockstep · Jun 15, 2013 at 03:31 PM
All you have to do is reducing the spawn delay:
void Update () {
if ( spawnTime < Time.time ) {
CreateCube ( spawnDistance + character.position.z );
spawnTime += delayBetweenSpawns;
if(delayBetweenSpawns > 0.25f)
delayBetweenSpanse -= 0.05f; //or whatever value you want.
}
}
Thanks you very much. It's working just how i want it to :-)
I'm glad that it works for you. Can you please accept this answer then, please? This will help people with similar problems to find the answer faster and prevent that people who want to help loose their time looking at an answered question. Thank you.
Answer by Kayn-SD · Jun 15, 2013 at 04:15 PM
A cheap way to do it would be to increase your delayBetweenSpawns value every time you wished to reduce the time between spawns. Alternatively, you'd reduce the value you're comparing spawnTime to.
This code adapts your current block and makes it dependant on Time.deltaTime instead. When you tap the Increase Difficulty box on the Inspector at runtime (or set it to true using code, whichever way you want), it will knock the timeBetweenCubes down a tenth of a second.
public float timeBetweenCubes = 0.75f;
public float spawnTime = 0;
public bool increaseDifficulty = false;
//
void Update (){
//
spawnTime += Time.deltaTime;
if ( spawnTime > timeBetweenCubes ) {
CreateCube ( spawnDistance + character.position.z );
spawnTime += delayBetweenSpawns;
spawnTime = 0;
}
if(increaseDifficulty){
increaseDifficulty = false;
timeBetweenCubes -= 0.1f;
if(timeBetweenCubes < 0.25f) timeBetweenCubes = 0.25f
}
//
}
Your answer
Follow this Question
Related Questions
How Increase And Decrease And Reset Spawn Rate Over 0 Answers
,Getting a Spawner Manager Script to reference a Spawner 1 Answer
How to make enemies spawn IN the ground without rising up automatically first? 1 Answer
I would like to make objects spawn and aim for a object that i have to dodge 1 Answer