How to add a wave in my spawn script
Okay basically I have a 2D game which I would like to spawn an prefab in cube, I have a spawning script but I would like to make this script into a wave spawning script. Where at a certain time the waves becomes faster then faster and so on. If there is anyway of doing this or an example you can give that would be amazing and i would appreciated it so much! Thank you! Here is my script:
using UnityEngine;
using System.Collections;
public class Atempt1 : MonoBehaviour {
public GameObject ObjectToSpawn;
public float RateOfSpawn = 1;
public float SpawnDelay = 20f;
private float nextSpawn = 0;
// Update is called once per frame
void Update () {
if (Time.time > nextSpawn) {
nextSpawn = Time.time + RateOfSpawn;
}
}
void Start(){
Invoke("Spawn", SpawnDelay);
}
void Spawn () {
// Random position within this transform
Vector3 rndPosWithin;
rndPosWithin = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
rndPosWithin = transform.TransformPoint(rndPosWithin * .5f);
Instantiate(ObjectToSpawn, rndPosWithin, transform.rotation);
Invoke("Spawn",SpawnDelay);
}
}
Answer by bubzy · Oct 01, 2015 at 10:31 PM
you can try something like this
WARNING
this quickly gets out of hand, so its worth checking the numbers or having some method of seeing if the previous wave was cleared first.
using UnityEngine;
using System.Collections;
public class spawner : MonoBehaviour {
// Use this for initialization
public GameObject spawnThing;
int spawnLevel = 1;
int spawnCount;
float spawnDelay = 3f; //seconds between waves
float minSpawnDelay = 0.1f; // the shortest time between waves possible
float currentDelay;
void Start () {
currentDelay = Time.time + spawnDelay;
}
// Update is called once per frame
void Update () {
if (Time.time > currentDelay) {
for(int i = 0; i < spawnLevel; i++)
{
spawnEnemy();
}
spawnLevel ++; //increase the spawn level
spawnDelay -= spawnLevel/10; //just an approximation, this will take 0.1 seconds off per level
if(spawnDelay < minSpawnDelay)
{
spawnDelay = minSpawnDelay; // this ensures that the spawn delay does not drop below this time.
}
currentDelay = Time.time + spawnDelay;
}
}
void spawnEnemy()
{
GameObject temp = Instantiate (spawnThing, transform.position, Quaternion.identity) as GameObject;
//do whatever you want with temp
Destroy (temp, 2);// this for debug to make sure it doesnt get overrun.
}
}
hope it helps :)
Your answer
Follow this Question
Related Questions
how can I add a maximum of enemies to be generated for each wave? 0 Answers
Wave spawner script 2 Answers
Spawning Bug. 1 Answer
Can someone help me add a wave to my Spawner script!!! 0 Answers
Spawning random objects not working 2 Answers