Last active
December 15, 2016 20:31
-
-
Save bjpeterdelacruz/c655e5a599842c422b01a4ff716778cd to your computer and use it in GitHub Desktop.
How to Spawn Hazards in Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class GameController : MonoBehaviour | |
{ | |
public GameObject hazard; | |
public Vector3 spawnValues; | |
public int hazardCount; | |
public float spawnWait; | |
public float startWait; | |
public float waveWait; | |
void Start () | |
{ | |
StartCoroutine (SpawnWaves ()); | |
} | |
IEnumerator SpawnWaves () | |
{ | |
yield return new WaitForSeconds (startWait); | |
while (true) | |
{ | |
for (int i = 0; i < hazardCount; i++) | |
{ | |
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z); | |
Quaternion spawnRotation = Quaternion.identity; | |
Instantiate (hazard, spawnPosition, spawnRotation); | |
yield return new WaitForSeconds (spawnWait); | |
} | |
yield return new WaitForSeconds (waveWait); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment