Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

how do i make the enemies spawn more freqent over time

Discussion in 'Scripting' started by stevenbos655, May 5, 2020.

  1. stevenbos655

    stevenbos655

    Joined:
    May 1, 2020
    Posts:
    14
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class enemySpawner : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private float spawnRadius = 7, time = 1.5f;
    9.     public GameObject[] enemies;
    10.     void Update()
    11.     {
    12.  
    13.     }
    14.     void Start()
    15.     {
    16.         StartCoroutine(SpawnAnEnemy());
    17.     }
    18.     IEnumerator SpawnAnEnemy()
    19.     {
    20.         Vector2 spawnPos = GameObject.Find("Player").transform.position;
    21.         spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
    22.         Instantiate(enemies[Random.Range(0, enemies.Length)], spawnPos, Quaternion.identity);
    23.         yield return new WaitForSeconds(time);
    24.         StartCoroutine(SpawnAnEnemy());
    25.     }
    26. }
    27.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You could slowly start reducing the amount of time to wait to spawn the next enemy:

    Code (CSharp):
    1.     IEnumerator SpawnAnEnemy()
    2.     {
    3.         Vector2 spawnPos = GameObject.Find("Player").transform.position;
    4.         spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
    5.         Instantiate(enemies[Random.Range(0, enemies.Length)], spawnPos, Quaternion.identity);
    6.         Debug.Log($"Waiting {time} seconds before spawning the next enemy");
    7.         yield return new WaitForSeconds(time);
    8.         time *= .9f;
    9.         StartCoroutine(SpawnAnEnemy());
    10.     }
    Adjust as desired.
     
    stevenbos655 likes this.
  3. stevenbos655

    stevenbos655

    Joined:
    May 1, 2020
    Posts:
    14
    thank you that really helped