Skip to content

Instantly share code, notes, and snippets.

@curious-username
Last active September 23, 2021 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curious-username/d5d927775b5a48cce64e4d8145c89db8 to your computer and use it in GitHub Desktop.
Save curious-username/d5d927775b5a48cce64e4d8145c89db8 to your computer and use it in GitHub Desktop.
spawnmanager power up handling
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
[SerializeField]
private GameObject _enemyPrefab;
[SerializeField]
private GameObject _enemyContainer;
private bool _stopSpawning = false;
[SerializeField]
private GameObject[] _powerups;
void Start()
{
StartCoroutine(SpawnRoutine());
StartCoroutine(SpawnPowerupRoutine());
}
// Update is called once per frame
void Update()
{
}
//spawn game objects every 5 seconds
//create a coroutine of type IEnumerator -- yield events
IEnumerator SpawnRoutine()
{
//while loop(infinate loop)
while (_stopSpawning == false)
{
Vector3 spawn = new Vector3(Random.Range(-8f, 8f), 7, 0);
GameObject newEnemy = Instantiate(_enemyPrefab, spawn, Quaternion.identity);
newEnemy.transform.parent = _enemyContainer.transform;
yield return new WaitForSeconds(5);
}
}
IEnumerator SpawnPowerupRoutine()
{
//every 3 - 7 seconds, spawn in a powerup
while (_stopSpawning == false) {
Vector3 spawn = new Vector3(Random.Range(-8f, 8f), 7, 0);
int randomPowerUp = Random.Range(0, 3);
Instantiate(_powerups[randomPowerUp], spawn, Quaternion.identity);
yield return new WaitForSeconds(Random.Range(3, 8));
}
}
public void OnPlayerDeath()
{
_stopSpawning = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment