- Home /
Quick State Machine Question c#
I was following the BZA tutorial for spawning enemies and everything is working great but is there a way (or did I miss something along the way?) to make it cycle and spawn a new enemy after a certain amount of time after the enemy in that spawn point dies?
here is the script at the moment.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ZombieSpawn : MonoBehaviour {
public enum State
{
Idle,
Initialize,
Setup,
SpawnMob
}
public GameObject[] zombiePrefab;
public GameObject[] spawnPoints; // array holds a ref to all spawnpoints in the world
public State state; // local variable that holds our current state.
void Awake()
{
state = ZombieSpawn.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()
{
Debug.Log ("Initialize Function");
if(!CheckForMobPrefabs ())
{
return;
}
if(!CheckForSpawnPoints())
{
return;
}
state = ZombieSpawn.State.Setup;
}
private void Setup()
{
Debug.Log("Setup Function");
state = ZombieSpawn.State.SpawnMob;
}
private void SpawnMob()
{
Debug.Log("Spawn Mob");
GameObject[] gos = AvailableSpawnPoints();
for(int cnt = 0; cnt < gos.Length; cnt++)
{
GameObject go = Instantiate(zombiePrefab[Random.Range(0, zombiePrefab.Length)],
gos[cnt].transform.position,
Quaternion.identity
) as GameObject;
go.transform.parent = gos[cnt].transform;
}
state = ZombieSpawn.State.Idle;
}
//check to see if we have at least one mob prefab to spawn
private bool CheckForMobPrefabs()
{
if(zombiePrefab.Length > 0)
return true;
else
return false;
}
//check to see if we have at least on spawn point
private bool CheckForSpawnPoints()
{
if(spawnPoints.Length > 0)
return true;
else
return false;
}
//generate a list of available spawnpoints that do not have any mobs childed to it
private GameObject[] AvailableSpawnPoints()
{
List<GameObject> gos = new List<GameObject>();
for(int cnt = 0; cnt < spawnPoints.Length; cnt++)
{
if(spawnPoints[cnt].transform.childCount == 0)
{
Debug.Log("Spawn Point Available");
gos.Add(spawnPoints[cnt]);
}
}
return gos.ToArray();
}
}
Answer by getyour411 · Jan 28, 2014 at 08:57 AM
Not directly from this - after the Spawn it goes to state Idle which has no supporting function shown. If I had to guess, I'd say the Zombie (on death) is supposed to call back to this State machine and set state to SpawnMob, which would look for a mobspawner with 0 transform children, find one, and spawn it. That could be a timing issue tho, since the Zombie (if it's still an active gameObject, and it would have to be to run its scripts) would still be a child. Look in mob code for OnDeath stuff and see if it clears it up, post back otherwise.
Sadly this was the only script I used from the tutorial series, I have my own scripts for everything else, But I'm sure I could add something into my enemy script that would force the state machine to reactivate upon it's death. Thanks for that advice, I didn't even think about that. I'll look into that later and post back with the results.
Awesome, It's kinda working now, I just need to implement an elapsed time before spawn only when the player is a certain distance away. Otherwise It will be one nonstop spawn fest at the spawn point :P
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Draw a handle in editor when key is pressed 2 Answers
Having trouble subscribing to events from external class 2 Answers