x


how do i make my spawn system spawn every 2 seconds then wait 10minutes?

so i have made mob generator just fine and they spawn but they only spawn once i wonder how i can allow them to spawn every 2 seconds then stop spawning after 10minutes wait 10minutes then spawn a new wave, at the moment it set to random range and selects a mob at random thanks for any help

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class MobGenerator : MonoBehaviour { public enum State { Idle, Initialize, Setup, SpawnMob } public GameObject[] mobPrefabs; //array to hold all mob prefabs public GameObject[] spawnPoints; //array of spawn points

 public State state;
 
 void Awake() {
     state = MobGenerator.State.Initialize;
 }
 // Use this for initialization
 IEnumerator Start () {
     while(true) {
         switch(state) {
         case State.Initialize:
             Initialize();
                 break;
         case State.Setup:
             Setup();
                 break;
         case State.SpawnMob:
             SpawnMob();
                 break;
             
         }
         
         yield return 0;
     }
 }
 private void Initialize() {
     
     if(!CheckForMobPrefabs())
         return;
     if(!CheckForMobSpawn())
         return;
     
     state = MobGenerator.State.Setup;
 }
 
 private void Setup() {
     state = MobGenerator.State.SpawnMob;
 }
 
 private void SpawnMob() {
     
     GameObject[] avaliable = AvailableSpawnPoints();
     
     for(int cnt = 0; cnt < avaliable.Length; cnt++) {
         GameObject SpawnMob = Instantiate(mobPrefabs[Random.Range(0, mobPrefabs.Length)],
                                     avaliable[cnt].transform.position,
                                     Quaternion.identity
                                     ) as GameObject;
         SpawnMob.transform.parent = avaliable[cnt].transform;
     
     state = MobGenerator.State.Idle;
 }

}

 //check for mobs
 private bool CheckForMobPrefabs() {
     if(mobPrefabs.Length > 0)
         return true;
     else
         return false;
 }
 
 private bool CheckForMobSpawn() {
     if(spawnPoints.Length > 0)
         return true;
     else
         return false;
 }
 
 private GameObject[] AvailableSpawnPoints() {
     List<GameObject> avaliable = new List<GameObject>();
     
     for(int cnt = 0; cnt < spawnPoints.Length; cnt++) {
         if( spawnPoints[cnt].transform.childCount == 0) {
             avaliable.Add(spawnPoints[cnt]);
         }
     }
     
     return avaliable.ToArray();
 }

}

more ▼

asked Dec 13, 2011 at 08:39 AM

quantumarchi gravatar image

quantumarchi
56 25 24 28

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Add a canSpawn var, and switch it between true to false. It would look like something like that.

 var canSpawn : boolean = true;
 var spawnTime : float = 600.0;
 var spawnClockStart : float = 0.0;
 var spawnClockEnd : float = 1.0;
 var spawnClockCur : float = 0.0;
 var lerpAmt : float = 0.0;
 
 function Update(){
 lerpAmt += Time.deltaTime / spawnTime;
     spawnClockCur = Mathf.Lerp (spawnClockStart, spawnClockEnd, lerpAmt);
 
   if(canSpawn &&
   spawnClockCur <= 1.0){
   canSpawn = false;
   lerpAmt = 0.0;
   spawClockCur = 0.0;
   }
   if(!canSpawn &&
   spawnClockCur <= 1.0){
   canSpawn = true;
   lerpAmt = 0.0;
   spawnClockCur = 0.0;
   }
 }

With this, just use the "canSpawn" as the condition to spawn your ennemies.

PS: I'm aware it's not a "Clean" code, it's just to show the principle of it.

more ▼

answered Dec 13, 2011 at 06:51 PM

Oninji gravatar image

Oninji
422 110 95 102

thanks now i didnt use code but it definitely help me understand what i was going for thanks very much works perfectly now

Dec 13, 2011 at 10:43 PM quantumarchi

No Problem :P Don't forget to choose as answer so the topic is set to "Answered" :P

Dec 14, 2011 at 03:30 AM Oninji
(comments are locked)
10|3000 characters needed characters left

Hello, i see that you use an FSM here. You can make a new State called "WaitTenMins" or something like that and set it after spawning the mobs. In the function you can try this:

 public float waitTime = 10F * 60F; //ten minutes
 private float nextWaveTime = 0.0F;
 
 void WaitTenMins() {
     if (Time.time > nextWaveTime) {
         state = MobGenerator.State.SpawnMobs;
     }    
 }


Of course you will need to set the nextWaveTime to Time.time + waitTime before you set the state to "WaitTenMins". Hope that helps.

more ▼

answered Dec 13, 2011 at 10:07 PM

eaperez gravatar image

eaperez
1 3 2 3

10min == 10*60sec == 600.0f ;)

Dec 14, 2011 at 04:26 AM Bunny83

oh yes i actually did do this but i did take the first guys answer but thanks very much added a few extra floats and a bool ill post it later :P

Dec 14, 2011 at 07:27 AM quantumarchi

my bad, edited to work for the ten minutes instead of 10 seconds XD. thanks.

Dec 14, 2011 at 01:08 PM eaperez
(comments are locked)
10|3000 characters needed characters left

I may be able to help, as I have done this in my game, but I use java script. Do you mind totally changing your script to javascript???

more ▼

answered Dec 13, 2011 at 11:05 PM

joeybbb gravatar image

joeybbb
1 12 7 7

Now why would you ever want to do that?

Dec 13, 2011 at 11:06 PM syclamoth
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2909
x897
x783
x148
x65

asked: Dec 13, 2011 at 08:39 AM

Seen: 1638 times

Last Updated: Dec 14, 2011 at 01:08 PM