Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SerializeField]
- private GameObject _enemyPrefab;
- [SerializeField]
- private List<GameObject> _powerUpList = new();
- [SerializeField]
- private GameObject _spawnContainer;
- private bool _alive = true;
- // Update is called once per frame
- void Start() {
- StartCoroutine(SpawnEnemyRotine());
- StartCoroutine(SpawnPowerUpRotine());
- }
- IEnumerator SpawnEnemyRotine() {
- while (_alive) {
- Vector3 posToSpawn = GetRandomPosX();
- GameObject newEnemy = Instantiate(_enemyPrefab,
- posToSpawn,
- Quaternion.identity);
- newEnemy.transform.parent = _spawnContainer.transform;
- yield return new WaitForSeconds(5);
- }
- }
- IEnumerator SpawnPowerUpRotine() {
- while (_alive) {
- Vector3 posToSpawn = GetRandomPosX();
- int randomObjIndex = Random.Range(0, _powerUpList.Count);
- Instantiate(_powerUpList[randomObjIndex],
- posToSpawn,
- Quaternion.identity);
- yield return new WaitForSeconds(Random.Range(3, 8));
- }
- }
- private static Vector3 GetRandomPosX() {
- return new(Random.Range(-9.41f, 9.41f), 7, 0);
- }
- public void StopSpawnning() {
- _alive = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement