c# - How to spawn enemies with a randomized speed -


i know how spawn enemies randomized speed. i've looked around on internet looking solutions, none of them seem come close (or maybe haven't looked hard enough, i'm not sure. if there's solution somewhere out there, please share it).

it'd great if have randomized speeds beginning after time has passed since start of game.

i have following code.

using unityengine; using system.collections; using system.collections.generic;  public class spawnenemies : monobehaviour {  //variable store enemy prefab public gameobject enemy; //array store spawn points public transform[] spawnpoints;  //variable countdown time each spawn more enemy prefabs can spawn public float spawntimer = 5.0f;  //variable store amount of original enemies spawned @ start of game private int nenemies = 1; //variable store previous spawn time private float prevspawntime = 1f; //variable store spawn delay time private float delay = 0f;  //list store number of spawn points list<int> spoints = new list<int>();  //update called once per frame void update() {     //variable store delayed spawn time, delayed spawn time equals current time since game has started minus previous spawn time     float dt = time.time - prevspawntime;     //if delayed spawn time more spawn delay time     if (dt > delay)      {         //call spawnenemy() function         spawnenemy();         //randomize spawn delay time, between 2 5 seconds         delay = random.range (2, 5);         //initialize previous spawn time current time         prevspawntime = time.time;     } }  //function spawn new enemies void spawnenemy() {     //decrease spawntimer each time 1 enemy prefab spawned     spawntimer -= 1;      //if 10 enemies spawned , number of current enemies spawning each time less 5     if (spawntimer % 10 == 0 && nenemies < 5)      {         //current number of enemies spawning each time +1         nenemies++;     }      //clear list of spawn points     spoints.clear ();     //insert number between 0-4 (the spawn points) starting @ 0 list of spawn points     spoints.insertrange (0, new int[]{0,1,2,3,4});      //as long there @ least 1 enemy spawning @ each time     (int = 0; < nenemies; i++)      {         //variable randomly store spawn position         int idx = random.range(0, spoints.count-1);         //initialize randomly stored spawn position list of spawn points         int pos = spoints[idx];         //remove randomly stored spawn position list no other enemy prefabs can spawn position         spoints.removeat(idx);         //instantiate enemy prefab @ randomly stored spawn position         instantiate (enemy, spawnpoints [pos].position, spawnpoints [pos].rotation);     } } 

} `

thank you!

using system.random rand;      void start()     {     rand = new system.random();     }      public ienumerator spawnatrandtime()     {       while(true)      {                yield return new waitforseconds(rand.next(0,100));        spawnenemy();      }     } 

this should work fine. use startcoroutine(spawnatrandtime()) in loop. when want stop spawning enemies call stopcoroutine().


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -