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

Question Dynamic spawn areas for enemy spawnings

Discussion in 'Scripting' started by Polarfist1, Jan 23, 2022.

  1. Polarfist1

    Polarfist1

    Joined:
    Aug 16, 2021
    Posts:
    15
    I have a game in which there is a map where enemies spawn. Currently, enemies spawn in a randomspawnpos in a random range across the map's x and z coordinates. What I want instead is a randomspawnpos that is dynamic.
    What this means is that enemies can spawn in different areas, only if that area has been unlocked by the player (buying a gate for access, or if its the standard one), else enemies cannot spawn in that area if it hasnt been unlocked.
    For the areas that have been unlocked, I want enemies to spawn in them randomly (ie if 3 enemies spawned next wave, 2 might spawn in Area 1 and 1 might spawn in area 2, 0 in area 3, 0 in area 4 as they have not been unlocked yet).
    Here is my code, note that any code in comments is my attempt at dynamic spawn position (basically pseudocode), the other code is there for the wave spawning or the static randomspawnpos. Also note that the spawnareas array has 2 elements in it currently, those being 2 empty objects with different coordinates. The array's list is controlled and changed in the script's component in the inspector (the script's parent is called SpawnPoint1 if that information is necessary):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class GameController : MonoBehaviour
    7. {
    8.     public int waveNumber = 0;
    9.     public int enemySpawnAmount = 0;
    10.     public int enemiesKilled = 0;
    11.  
    12.     //public GameObject[] spawnareas;
    13.  
    14.     public GameObject enemy;
    15.  
    16.     public float maxX;
    17.     public float maxZ;
    18.  
    19.     private Enemy enemyScript;
    20.     private NavMeshAgent enemyNavMesh;
    21.  
    22.     //public int spawnareasx = Random.Range(spawnareas[0, 1].transform.position.x);
    23.     //public int spawnareasz = Random.Range(spawnareas[0, 1].transform.position.z);
    24.     void Start()
    25.     {
    26.         enemyScript = enemy.GetComponent<Enemy>();
    27.         enemyNavMesh = enemy.GetComponent<NavMeshAgent>();
    28.         //InvokeRepeating("spawnEnemy", 1f, 10f);
    29.         //spawnareas[1].SetActive(false); this is not unlocked yet by the pkayer, spawnareas[0] however is not SetActive(false) as that is the standard spawn area)
    30.         StartWave();
    31.     }
    32.     void Update()
    33.     {
    34.  
    35.         if(Input.GetKeyDown(KeyCode.P))
    36.         {
    37.             spawnEnemy();
    38.         }
    39.  
    40.         if (enemiesKilled >= enemySpawnAmount)
    41.         {
    42.             NextWave();
    43.         }
    44.     }
    45.  
    46.     void spawnEnemy()
    47.     {
    48.         float RandomX = Random.Range(-maxX, maxX);
    49.         float RandomZ = Random.Range(-maxZ, maxZ);
    50.         Vector3 randomSpawnPos = new Vector3(RandomX, 10f, RandomZ);
    51.         //Vector3 randomSpawnPos2 = new Vector3(spawnareasx, 10f, spawnareasz);
    52.  
    53.         Instantiate(enemy, randomSpawnPos, Quaternion.identity);
    54.     }
    55.  
    56.  
    57.     void StartWave()
    58.     {
    59.         waveNumber = 1;
    60.         enemySpawnAmount = 2;
    61.         enemiesKilled = 0;
    62.  
    63.         for(int i = 0; i < enemySpawnAmount; i++)
    64.         {
    65.             Invoke("spawnEnemy", 2);
    66.            
    67.         }
    68.     }
    69.  
    70.     public void NextWave()
    71.     {
    72.         waveNumber++;
    73.         enemySpawnAmount += 2;
    74.         enemiesKilled = 0;
    75.         enemyScript.health += 2f;
    76.        
    77.         if(enemyNavMesh.speed < 15f)
    78.         {
    79.             enemyNavMesh.speed += 0.1f;
    80.         }
    81.        
    82.         if(waveNumber % 5 == 0)
    83.         {
    84.             enemyScript.enemyScore += 10f;
    85.         }
    86.        
    87.         for (int i = 0; i < enemySpawnAmount; i++)
    88.         {
    89.             Invoke("spawnEnemy", 2);
    90.         }
    91.     }
    92. }
    93.  
     
  2. Polarfist1

    Polarfist1

    Joined:
    Aug 16, 2021
    Posts:
    15
    Also, does setactive(false) actually remove the gameobject in the array from the pool of choosable spawn locations?