Hello Community!
I would like to share my little part of my project. I called it “RandomEnemySpawner”. This allows to you to spawn enemy’s to your world via SpawnPoints.
The system is very simple, when your mob is dead the system will wait. After add a new enemy to the right position. Like the World of Warcraft System.
Its a very basic script and not the BEST.
So how to use:
- Create a new GameObject to your scence and attach the RandomEnemySpawner script to it.
- Add your enemy prefab to the RandomEnemySpawner.
- Create a new GameObject and create a new Tag which i called “SpawnPoint” (you can change it in the script)
- Clone this GamObject for some times and place it random place. The characters will spawning at those points.
-
- NOT Important, but i recommend to create one extra GameObject for the spawned Enemy. So the enemys don’t will be the top of your hierarchy. So Create “Enemys Parent” GameObject and create a new Tag and named it “UnitParent”(The Tag is Important).
In this script there is a GamePause Stage. So if you use a some sort of pause game system in you game, when your game is paused this script will disable its self and stop calculating the next respawn time(its a bit odd system).
So thats all folks. I hope you can understand what im writing. If you have a Question feel free and ask.
Heres the script or the Download link
using UnityEngine;
using System.Collections;
/// <summary>
/// Created by Xii from Protonox
/// Random enemy spawner script will management the spawning system
/// also look out for the QuestMonster System.
/// </summary>
public class RandomEnemySpawner : MonoBehaviour {
#region "Variables"
private static RandomEnemySpawner _instance;
public static RandomEnemySpawner Instance {
get {
if(_instance == null)
_instance = FindObjectOfType(typeof(RandomEnemySpawner)) as RandomEnemySpawner;
return _instance;
}
}
//The GameObjects Tag where the mobs will spawning...
public string spawnTag = "SpawnPoint";
public SpawnPoint[] spawnPoints;
//Use Random length of all spawnpoints at start
public bool useRandomAtStart = false;
//One type of enemy can spawn
public GameObject enemyPrefab;
//Don't allow the system so spawn the enemys to the top of the Hierarhy
private Transform _parentGo;
public string parentTag = "UnitParent";
private bool _isGamePaused;
private bool _debug = false;
#endregion
// Use this for initialization
void Start () {
GameObject[] spawns = GameObject.FindGameObjectsWithTag(spawnTag);
_parentGo = GameObject.FindWithTag(parentTag).transform;
if(_parentGo == null)
Debug.LogWarning("There is no parent so the enemy will Spawn at the top of the Hierarchy");
if(_debug)
Debug.Log(" - RandomEnemySpawner.Start -> Found SpawnPoints: " + spawns.Length);
spawnPoints = new SpawnPoint[spawns.Length];
for(int i=0; i< spawns.Length; i++) {
SpawnPoint sp = spawns[i].AddComponent<SpawnPoint>();
sp.id = i;
sp.go = spawns[i];
sp.allocated = false;
spawnPoints[i] = sp;
}
if(spawns.Length > 0)
SpawnRandomEnemy();
}
void SpawnRandomEnemy() {
if(_isGamePaused)
return;
int spawnCount = spawnPoints.Length;
if(useRandomAtStart)
spawnCount = Random.Range(1, spawnPoints.Length);
if(_debug)
Debug.Log(" - RandomEnemySpawner.SpawnRandomEnemy -> " + spawnCount + " Enemy will spawn!");
for(int i=0; i<spawnCount; i++) {
int index = i;
if(useRandomAtStart)
index = Random.Range(0, spawnPoints.Length);
if(!spawnPoints[index].allocated) {
spawnPoints[index].allocated = true;
SpawnEnemyAtSpawnPos(index);
}
}
}
public void OnKillEnemy(int id) {
if(_debug)
Debug.Log(" - RandomEnemySpawner.OnKillEnemy -> Free Slot at " + id);
spawnPoints[id].allocated = false;
spawnPoints[id].Invoke("HeySpawnMe", spawnPoints[id].reSpawnTime);
}
public void SpawnEnemyAtSpawnPos(int id) {
GameObject enemy = Instantiate(enemyPrefab, spawnPoints[id].go.transform.position, Quaternion.identity) as GameObject;
IhaveASpawn spawn = enemy.AddComponent<IhaveASpawn>();
spawn.id = id;
if(_parentGo != null)
enemy.transform.parent = _parentGo;
TargetSystem.Instance.AddTarget(enemy.transform);
if(_debug)
Debug.Log(" - RandomEnemySpawner.OnKillEnemy -> Spawn a new Enemy at " + id);
}
//Not Used function because it's old and not support the pause stuffs
IEnumerator Spawn(int id) {
yield return new WaitForSeconds(spawnPoints[id].reSpawnTime);
SpawnEnemyAtSpawnPos(id);
}
//Pause System
public void PauseGame() {
_isGamePaused = true;
if(_debug)
Debug.Log(" - RandomEnemySpawner.PauseGame(" + gameObject.name + ") --> PAUSED");
for(int i=0; i<spawnPoints.Length; i++) {
if(spawnPoints[i].IsInvoking("HeySpawnMe")) {
spawnPoints[i].CancelInvoke("HeySpawnMe");
spawnPoints[i].interrupted = true;
}
}
}
public void UnPauseGame() {
_isGamePaused = false;
if(_debug)
Debug.Log(" - RandomEnemySpawner.UnPauseGame(" + gameObject.name + ") --> Restart");
for(int i=0; i<spawnPoints.Length; i++) {
if(spawnPoints[i].interrupted) {
spawnPoints[i].Invoke("HeySpawnMe", spawnPoints[i].reSpawnTime);
spawnPoints[i].interrupted = false;
}
}
}
}
/// <summary>
/// Spawn point script will attached to the point GameObject which will be spawned the Mobs
/// also not allow to spawn two mob once a time to one GameObject.
/// </summary>
public class SpawnPoint : MonoBehaviour {
//The Idetifier for the SpawnPoint[] array
public int id;
//The GameObject it self :)
public GameObject go;
//If there is an alive mob at the spawnPoint its TRUE.
//If there is no alive mob at the spawnPoints its FALSE.
public bool allocated;
//This variable for unique spawnTime
public float reSpawnTime;
//This variable for the dont allow to respawn the mob while the game in PAUSE Stage
public bool interrupted = false;
public SpawnPoint() {
id = -1;
allocated = false;
reSpawnTime = Random.Range(20, 30);
}
public SpawnPoint(GameObject gos, bool a, int i) {
id = i;
go = gos;
allocated = a;
reSpawnTime = Random.Range(20, 30);
}
public void HeySpawnMe() {
RandomEnemySpawner.Instance.SpawnEnemyAtSpawnPos(id);
}
}
/// <summary>
/// Ihave A spawn script will attached to the enemy when it created
/// and when the enemy is killed this script will send a message to the Random enemy spawner script
/// to my place is free NOT.
/// </summary>
public class IhaveASpawn : MonoBehaviour {
//The Idetifier for the SpawnPoint[] array
public int id;
public IhaveASpawn(int i) {
id = i;
}
//You need to call this function when the enemy is Die
//Put this code for your AI script
// like this way
//
//RandomSpawnerSystem
//IhaveASpawn spawnSender;
//if(spawnSender = GetComponent<IhaveASpawn>())
// spawnSender.ImKilled();
//
public void ImKilled() {
RandomEnemySpawner.Instance.OnKillEnemy(id);
}
}