Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[FREE] Enemy Spawner System

Discussion in 'Community Learning & Teaching' started by Xian55, Jul 12, 2013.

Thread Status:
Not open for further replies.
  1. Xian55

    Xian55

    Joined:
    Jul 6, 2012
    Posts:
    12
    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:
    1. Create a new GameObject to your scence and attach the RandomEnemySpawner script to it.
    2. Add your enemy prefab to the RandomEnemySpawner.
    3. Create a new GameObject and create a new Tag which i called "SpawnPoint" (you can change it in the script)
    4. Clone this GamObject for some times and place it random place. The characters will spawning at those points.
    5. 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


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. /// <summary>
    7. /// Created by Xii from Protonox
    8. /// Random enemy spawner script will management the spawning system
    9. /// also look out for the QuestMonster System.
    10. /// </summary>
    11.  
    12. public class RandomEnemySpawner : MonoBehaviour {
    13.  
    14.     #region "Variables"
    15.  
    16.     private static RandomEnemySpawner _instance;
    17.     public static RandomEnemySpawner Instance {
    18.         get {
    19.             if(_instance == null)
    20.                 _instance = FindObjectOfType(typeof(RandomEnemySpawner)) as RandomEnemySpawner;
    21.            
    22.             return _instance;
    23.         }
    24.     }
    25.    
    26.    
    27.     //The GameObjects Tag where the mobs will spawning...
    28.     public string spawnTag = "SpawnPoint";
    29.     public SpawnPoint[] spawnPoints;
    30.    
    31.    
    32.     //Use Random length of all spawnpoints at start
    33.     public bool useRandomAtStart = false;
    34.    
    35.    
    36.     //One type of enemy can spawn
    37.     public GameObject enemyPrefab;
    38.  
    39.     //Don't allow the system so spawn the enemys to the top of the Hierarhy
    40.     private Transform _parentGo;
    41.     public string parentTag = "UnitParent";
    42.    
    43.    
    44.     private bool _isGamePaused;
    45.    
    46.     private bool _debug = false;
    47.    
    48.     #endregion
    49.    
    50.     // Use this for initialization
    51.     void Start () {
    52.         GameObject[] spawns = GameObject.FindGameObjectsWithTag(spawnTag);
    53.        
    54.        
    55.         _parentGo = GameObject.FindWithTag(parentTag).transform;
    56.         if(_parentGo == null)
    57.             Debug.LogWarning("There is no parent so the enemy will Spawn at the top of the Hierarchy");
    58.        
    59.         if(_debug)
    60.             Debug.Log(" - RandomEnemySpawner.Start -> Found SpawnPoints: " + spawns.Length);
    61.        
    62.        
    63.         spawnPoints = new SpawnPoint[spawns.Length];
    64.         for(int i=0; i< spawns.Length; i++) {
    65.             SpawnPoint sp = spawns[i].AddComponent<SpawnPoint>();
    66.             sp.id = i;
    67.             sp.go = spawns[i];
    68.             sp.allocated = false;
    69.            
    70.             spawnPoints[i] = sp;
    71.         }
    72.        
    73.         if(spawns.Length > 0)
    74.             SpawnRandomEnemy();
    75.     }
    76.    
    77.    
    78.     void SpawnRandomEnemy() {
    79.        
    80.         if(_isGamePaused)
    81.             return;
    82.        
    83.         int spawnCount = spawnPoints.Length;
    84.        
    85.         if(useRandomAtStart)
    86.             spawnCount = Random.Range(1, spawnPoints.Length);
    87.        
    88.         if(_debug)
    89.             Debug.Log(" - RandomEnemySpawner.SpawnRandomEnemy -> " + spawnCount + " Enemy will spawn!");
    90.        
    91.         for(int i=0; i<spawnCount; i++) {
    92.            
    93.             int index = i;
    94.            
    95.             if(useRandomAtStart)
    96.                 index = Random.Range(0, spawnPoints.Length);
    97.            
    98.             if(!spawnPoints[index].allocated) {
    99.                 spawnPoints[index].allocated = true;
    100.                
    101.                 SpawnEnemyAtSpawnPos(index);
    102.             }
    103.            
    104.         }
    105.     }
    106.    
    107.    
    108.     public void OnKillEnemy(int id) {
    109.        
    110.         if(_debug)
    111.             Debug.Log(" - RandomEnemySpawner.OnKillEnemy -> Free Slot at " + id);
    112.        
    113.         spawnPoints[id].allocated = false;
    114.         spawnPoints[id].Invoke("HeySpawnMe", spawnPoints[id].reSpawnTime);
    115.     }
    116.    
    117.    
    118.     public void SpawnEnemyAtSpawnPos(int id) {
    119.         GameObject enemy = Instantiate(enemyPrefab, spawnPoints[id].go.transform.position, Quaternion.identity) as GameObject;
    120.        
    121.         IhaveASpawn spawn = enemy.AddComponent<IhaveASpawn>();
    122.         spawn.id = id;
    123.        
    124.         if(_parentGo != null)
    125.             enemy.transform.parent = _parentGo;
    126.        
    127.         TargetSystem.Instance.AddTarget(enemy.transform);
    128.        
    129.         if(_debug)
    130.             Debug.Log(" - RandomEnemySpawner.OnKillEnemy -> Spawn a new Enemy at " + id);
    131.     }
    132.    
    133.    
    134.     //Not Used function because it's old and not support the pause stuffs
    135.     IEnumerator Spawn(int id) {
    136.         yield return new WaitForSeconds(spawnPoints[id].reSpawnTime);
    137.         SpawnEnemyAtSpawnPos(id);
    138.     }
    139.    
    140.    
    141.     //Pause System
    142.     public void PauseGame() {
    143.         _isGamePaused = true;
    144.        
    145.         if(_debug)
    146.             Debug.Log(" - RandomEnemySpawner.PauseGame(" + gameObject.name + ") --> PAUSED");
    147.        
    148.        
    149.         for(int i=0; i<spawnPoints.Length; i++) {
    150.             if(spawnPoints[i].IsInvoking("HeySpawnMe")) {
    151.                 spawnPoints[i].CancelInvoke("HeySpawnMe");
    152.                 spawnPoints[i].interrupted = true;
    153.             }
    154.         }
    155.        
    156.     }
    157.    
    158.     public void UnPauseGame() {
    159.         _isGamePaused = false;
    160.        
    161.         if(_debug)
    162.             Debug.Log(" - RandomEnemySpawner.UnPauseGame(" + gameObject.name + ") --> Restart");
    163.        
    164.         for(int i=0; i<spawnPoints.Length; i++) {
    165.             if(spawnPoints[i].interrupted) {
    166.                 spawnPoints[i].Invoke("HeySpawnMe", spawnPoints[i].reSpawnTime);
    167.                 spawnPoints[i].interrupted = false;
    168.             }
    169.         }
    170.        
    171.     }
    172.  
    173. }
    174.  
    175.  
    176. /// <summary>
    177. /// Spawn point script will attached to the point GameObject which will be spawned the Mobs
    178. /// also not allow to spawn two mob once a time to one GameObject.
    179. /// </summary>
    180.  
    181. public class SpawnPoint : MonoBehaviour {
    182.     //The Idetifier for the SpawnPoint[] array
    183.     public int id;
    184.    
    185.     //The GameObject it self :)
    186.     public GameObject go;
    187.    
    188.     //If there is an alive mob at the spawnPoint its TRUE.
    189.     //If there is no alive mob at the spawnPoints its FALSE.
    190.     public bool allocated;
    191.    
    192.     //This variable for unique spawnTime
    193.     public float reSpawnTime;
    194.    
    195.     //This variable for the dont allow to respawn the mob while the game in PAUSE Stage
    196.     public bool interrupted = false;
    197.    
    198.     public SpawnPoint() {
    199.         id = -1;
    200.         allocated = false;
    201.         reSpawnTime = Random.Range(20, 30);
    202.     }
    203.    
    204.     public SpawnPoint(GameObject gos, bool a, int i) {
    205.         id = i;
    206.         go = gos;
    207.         allocated = a;
    208.         reSpawnTime = Random.Range(20, 30);
    209.     }
    210.    
    211.     public void HeySpawnMe() {
    212.         RandomEnemySpawner.Instance.SpawnEnemyAtSpawnPos(id);  
    213.     }
    214. }
    215.  
    216.  
    217. /// <summary>
    218. /// Ihave A spawn script will attached to the enemy when it created
    219. /// and when the enemy is killed this script will send a message to the Random enemy spawner script
    220. /// to my place is free NOT.
    221. /// </summary>
    222.  
    223. public class IhaveASpawn : MonoBehaviour {
    224.     //The Idetifier for the SpawnPoint[] array
    225.     public int id;
    226.    
    227.     public IhaveASpawn(int i) {
    228.         id = i;
    229.     }
    230.    
    231.    
    232.     //You need to call this function when the enemy is Die
    233.     //Put this code for your AI script
    234.     // like this way
    235.     //
    236.     //RandomSpawnerSystem
    237.     //IhaveASpawn spawnSender;
    238.     //if(spawnSender = GetComponent<IhaveASpawn>())
    239.     //  spawnSender.ImKilled();
    240.     //
    241.     public void ImKilled() {
    242.         RandomEnemySpawner.Instance.OnKillEnemy(id);
    243.     }
    244. }
     
    Last edited: Aug 29, 2013
  2. Xian55

    Xian55

    Joined:
    Jul 6, 2012
    Posts:
    12
    Bump, nobody intrested about this thread, or the idea?
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The teaching category is not appropriate for this; the assets&asset store category would be better.

    --Eric
     
Thread Status:
Not open for further replies.