- Home /
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();
}
}
Answer by Oninji · Dec 13, 2011 at 06:51 PM
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.
thanks now i didnt use code but it definitely help me understand what i was going for thanks very much works perfectly now
No Problem :P Don't forget to choose as answer so the topic is set to "Answered" :P
Answer by eaperez · Dec 13, 2011 at 10:07 PM
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.
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
my bad, edited to work for the ten $$anonymous$$utes ins$$anonymous$$d of 10 seconds XD. thanks.
Answer by joeybbb · Dec 13, 2011 at 11:05 PM
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???
Your answer
Follow this Question
Related Questions
Make enemy spawn only if the previous one spawned it out of the way. 0 Answers
spawn timer problem 2 Answers
Instantiate 1 at a time at each transform in array 1 Answer
How to make something happen over time? 1 Answer
Instantiate over time? 1 Answer