using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemySpawn : MonoBehaviour { //variable to set the game object to the player public GameObject player; //public variable to set the range the player has to be in to activate spawner public float enemyRange = 5; //game object for the enemy(prefab) that will be spawned public GameObject spawnedEnemy; //Interval between enemy spawns public float spawnInterval = 2; //timer variable float timer = 0f; //Number of enemies to spawn in each interval public int numberOfEnemies = 1; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { //Check to see if the player is within range if (Vector3.Distance(transform.position, player.transform.position) < enemyRange) { //count up on the timer timer += Time.fixedDeltaTime; //if timer has counted above interval execute the if command if (timer >= spawnInterval) { //create the number of enemies that the designer has set for(int i = 0; i < numberOfEnemies; i++) { //create enemy at position of spawner GameObject sEnemy = Instantiate(spawnedEnemy, transform.position, Quaternion.identity); //reset timer back to 0 timer = 0; } } } } }