make enemy cars spawn faster over time!
hi i got this script that spawns 5 cars every 0.8 seconds but i want them to spawn faster and faster after time so maybe after every 20 seconds the the 5 cars spawn every 0.7 seconds and i want it to stop to accelerate when it is 0.4 please help me i am having big trouble with this, here is my script:
public GameObject[] Cars;
int carNo;
public float delayTimer = 1f;
float Timer;
void Start () {
Timer = delayTimer;
}
void Update () {
Timer -= Time.deltaTime;
if (Timer <= 0)
{
Vector3 carPos = new Vector3(Random.Range(-2.2f, 2.2f), transform.position.y, transform.position.z);
carNo = Random.Range(0,5);
Instantiate(Cars[carNo], carPos, transform.rotation);
Timer = delayTimer;
}
}
the script works great and it spawns the objects but i want it to spawn faster and faster so the game gets more difficult .
You need to think a bit about the game design first. There are several ways: you can look at the time remaining, the time spent on the level, or a fixed time step to modify the spawn rate.
so every 20 seconds you want reduce the timespan between spanw by 0.1 is that correct?(with max at 0.4)
You need to implement another time as the one already in you script but initializing at 20f I suggest you take more explicit names for the variables.
//Global
public float delayTimer2 = 20f;
float Timer2;
//In Update
Timer2 -= Time.deltaTime;
if (Timer2 <= 0)
{
if( delayTimer>0.5f){
delayTimer -=0.1f
}
Timer2 = delayTimer2 ;
}
this above code will go in update as well
Let me try that in game and thanks for answering early
It works but it goes directly to 0.5 i want to go from 0.8 to 0.7 to 0.6 and then 0.5 and i want this to happened every 20 seconds here is the script again:
//Acceleration...
public float delayTimer2 = 20f;
float Timer2;
public GameObject[] Cars;
int carNo;
public float delayTimer = 1f;
float Timer;
void Start () {
Timer = delayTimer;
}
void Update () {
Timer2 -= Time.deltaTime;
Timer2 = delayTimer2;
if(Timer2 <= 0)
{
if(delayTimer > 0.5f)
{
delayTimer -= 0.1f;
}
}
Timer -= Time.deltaTime;
if (Timer <= 0)
{
Vector3 carPos = new Vector3(Random.Range(-2.2f, 2.2f), transform.position.y, transform.position.z);
carNo = Random.Range(0,5);
Instantiate(Cars[carNo], carPos, transform.rotation);
Timer = delayTimer;
}
}
Answer by anggarp · Feb 18, 2016 at 01:53 PM
u problem same with me because we use the same code exactly. are u solve the problem yet? please tell me
Answer by patrickharvey · Aug 29, 2017 at 12:47 PM
can someone can solve this problem? it will help alot for who will helped .
Your answer
Follow this Question
Related Questions
Time Based Scoring - 2D Endless Runner ,Time based scoring system 0 Answers
2D Enemy Ai 0 Answers
How can I do an Enemy while He's walking his HEAD always see the Target? 1 Answer
How to add a timer to say wait for 5 s from now if bools are true? 1 Answer
WheelCollider motorTorque acceleration with ps4 triggers 0 Answers