Advertisement
EdGr87

Untitled

Dec 21st, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1.  [SerializeField]
  2.     private GameObject _enemyPrefab;
  3.     [SerializeField]
  4.     private List<GameObject> _powerUpList = new();
  5.     [SerializeField]
  6.     private GameObject _spawnContainer;
  7.  
  8.     private bool _alive = true;
  9.  
  10.     // Update is called once per frame
  11.     void Start() {
  12.         StartCoroutine(SpawnEnemyRotine());
  13.         StartCoroutine(SpawnPowerUpRotine());
  14.     }
  15.  
  16.     IEnumerator SpawnEnemyRotine() {
  17.  
  18.         while (_alive) {
  19.             Vector3 posToSpawn = GetRandomPosX();
  20.             GameObject newEnemy = Instantiate(_enemyPrefab,
  21.                  posToSpawn,
  22.                  Quaternion.identity);
  23.             newEnemy.transform.parent = _spawnContainer.transform;
  24.             yield return new WaitForSeconds(5);
  25.         }
  26.     }
  27.  
  28.     IEnumerator SpawnPowerUpRotine() {
  29.  
  30.         while (_alive) {
  31.             Vector3 posToSpawn = GetRandomPosX();
  32.             int randomObjIndex = Random.Range(0, _powerUpList.Count);
  33.             Instantiate(_powerUpList[randomObjIndex],
  34.                  posToSpawn,
  35.                  Quaternion.identity);
  36.             yield return new WaitForSeconds(Random.Range(3, 8));
  37.         }
  38.     }
  39.  
  40.  
  41.     private static Vector3 GetRandomPosX() {
  42.         return new(Random.Range(-9.41f, 9.41f), 7, 0);
  43.     }
  44.  
  45.     public void StopSpawnning() {
  46.         _alive = false;
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement