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

Spawn-Manager with Waves and Random spawnpoint

Discussion in 'Scripting' started by SQ69, Aug 29, 2022.

  1. SQ69

    SQ69

    Joined:
    Aug 17, 2022
    Posts:
    3
    im new and try to make my project from beginning.

    I made A random Spawn position, a IEnumerator to wait between waves. The problems are:

    1. Enemys (10) spawn at the same point

    2. After waveNum increase, the enemy count dont reset

    3. It dont spawn new Enemys
    Code (CSharp):
    1. void Start()
    2.     {
    3.         player = GameObject.FindGameObjectWithTag("Player").transform;
    4.         Spawn();
    5.     }
    6.  
    7.     void Update()
    8.     {
    9.         enemyCount = FindObjectsOfType<Enemy>().Length;
    10.         WaitAndSpawn();
    11.     }
    *this code generate random spawn position for enemys:

    Code (CSharp):
    1. private Vector3 GenerateSpawnPos(){
    2.  
    3.  float spawnPosX = Random.Range(-spawnRange, spawnRange);
    4.  float spawnPosZ = Random.Range(-spawnRange, spawnRange);
    5.  
    6.  Vector3 spawnPos = new Vector3(spawnPosX, 0.91f, spawnPosZ);
    7.  
    8.  return spawnPos;}
    *This is IEnumerator:

    Code (CSharp):
    1. private IEnumerator WaitAndSpawn() {
    2.  
    3.  yield return new WaitForSeconds(waitTime);
    4.  Spawn();}
    *And the last Spawn:

    Code (CSharp):
    1. private void Spawn(){
    2.  
    3.  int index = Random.Range(0, enemys.Length);
    4.  Vector3 enemySpawn = GenerateSpawnPos();
    5.  while (Vector3.Distance(player.position, enemySpawn) < treshhold)
    6.  {
    7.     enemySpawn = GenerateSpawnPos();
    8.  }
    9.  for (; enemyCount > 0; enemyCount--)
    10.  {
    11.     Instantiate(enemys[index], enemySpawn, enemys[index].transform.rotation);
    12.  }
    13.  if (enemyCount == 0)
    14.  {
    15.     enemyCount = 10;
    16.     waveNumber++;
    17.  }
    18.  }
    My goal is to spawn enemys at random location + outside the radius of player (*4) and spawn timer should be 10 seconds between waves. *for test i made 2.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    First thing I notice is you have WaitAndSpawn as a coroutine, but you aren't calling it properly. Make sure you look into coroutines and how they should be called. (hint, StartCoroutine)

    Next, you're calling it from Update, which is a bad move since it would start it every single frame. Sure, you have a wait in there, but you'll end up with so many WaitAndSpawn waiting for their yields to end just so they will call Spawn.

    Chances are good that this is creating issues for you. You might want a bool instead that gets set so WaitAndSpawn doesn't queue up a call per frame. Or better yet, probably don't call it from Update.
     
    SQ69 likes this.
  3. SQ69

    SQ69

    Joined:
    Aug 17, 2022
    Posts:
    3
    okay I made it by my self

    Code (CSharp):
    1. void SpawnEnemyWave(int enemysToSpawn)
    2.     {
    3.         int index = Random.Range(0, enemys.Length);
    4.      
    5.         for (int i = 0; i < enemysToSpawn; i++)
    6.         {
    7.             Instantiate(enemys[index], GenerateSpawnPos(), enemys[index].transform.rotation);
    8.         }
    9.     }
    I changed whole Spawn

    and placed some in Update methode :D works fine !;)
     
  4. SQ69

    SQ69

    Joined:
    Aug 17, 2022
    Posts:
    3
    Thx for notice;)