Search Unity

Respawn enemy tool

Discussion in 'Editor & General Support' started by henmachuca, Oct 14, 2016.

  1. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    105
    Hello,

    I have been trying to figure out a way to make this respawn enemy tool be able to start respawning the mob after I killed them in game! It calculates the starting number in game and a max count at the same time, but I can't figure a way to set that respawned obj to a false state after it is killed. Can someone help me?
    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using System.Collections.Generic;
    5. using Random = UnityEngine.Random;
    6.  
    7. //vai representar um range min e max entre 02 valores estipulados
    8. [Serializable]
    9. public class Range
    10. {
    11.     public float min;
    12.     public float max;
    13.     public Range ()
    14.     {
    15.         min = 0;
    16.         max = 0;
    17.     }
    18.     public Range (float a, float b)
    19.     {
    20.         min = a;
    21.         max = b;
    22.     }
    23.     public float getValueInRange()
    24.     {
    25.         return Random.Range(min, max);
    26.     }
    27. }
    28.  
    29.  
    30. public class SpawnArea : MonoBehaviour {
    31.  
    32.     public Spawnable _prefab;
    33.     public Collider _area;
    34.     public Collider _ground;
    35.     public int _initialCount = 5;
    36.     public int _maxCount = 10;
    37.     public Range _spawnTime = new Range(10, 60);
    38.  
    39.     private mob enemy;
    40.  
    41.     private List<Spawnable> _unused = new List<Spawnable>();
    42.     private List<Spawnable> _used = new List<Spawnable>();
    43.     [SerializeField]private float _timeUntilNextSpawn;
    44.  
    45.     // Use this for initialization
    46.     void Start ()
    47.     {
    48.  
    49.        //Create our inicial list of spawnables
    50.         for (int i=0; i < _maxCount; i++)
    51.         {
    52.             Spawnable spawnable = Instantiate(_prefab);
    53.             spawnable._area = this;
    54.             spawnable.transform.parent = this.transform;
    55.             spawnable.gameObject.SetActive(false);
    56.             _unused.Add(spawnable);
    57.         }
    58.  
    59.         //Spawn the inital number of spawnables
    60.         for (int i=0; i < _initialCount; i++)
    61.         {
    62.             SpawnMob();
    63.         }
    64.  
    65.         //Initialize the spawn timer
    66.         _timeUntilNextSpawn = _spawnTime.getValueInRange();
    67.     }
    68.    
    69.     // Update is called once per frame
    70.     void Update ()
    71.     {
    72.         //Update the spawntime
    73.         _timeUntilNextSpawn -= Time.deltaTime;
    74.         if (_timeUntilNextSpawn <= 0)
    75.         {
    76.             SpawnMob();
    77.             _timeUntilNextSpawn = _spawnTime.getValueInRange();
    78.         }
    79.  
    80.         if (_prefab.GetComponent<mob>().health <= 0)
    81.         {
    82.             Debug.Log("SPAWNANDO");
    83.             Instantiate(_prefab).gameObject.SetActive(false);
    84.             _used.Remove(_prefab);
    85.             _unused.Add(_prefab);
    86.         }
    87.     }
    88.  
    89.     //"Create" a new spawnable by moving one of the unused spawnables into the used list
    90.     public void SpawnMob()
    91.     {
    92.         if (_unused.Count > 0)
    93.         {
    94.             //Get the first unused spawnable
    95.             Spawnable spawnable = _unused[0];
    96.             _unused.RemoveAt(0);
    97.             _used.Add (spawnable);
    98.  
    99.             //Activate this spawnable
    100.             GameObject obj = spawnable.gameObject;
    101.             obj.SetActive(true);
    102.  
    103.             //Generate a random position within the spawn area
    104.             Vector3 pos = MathUtils.PointInsideArea(_area);
    105.             pos = MathUtils.SnapToTheGround(pos, _ground);
    106.             obj.transform.position = pos;
    107.  
    108.             //Make the spawnable look in a random orientation
    109.             Vector3 lookPos = pos + Random.insideUnitSphere;
    110.             lookPos.y = pos.y;
    111.             obj.transform.LookAt(lookPos, Vector3.up);
    112.  
    113.             //call spawn on the spawnable
    114.             spawnable.Spawn();
    115.         }
    116.     }
    117.