Search Unity

How can i spawn Multiple enemy in multiple spawn point

Discussion in 'Scripting' started by ashishkushwaha, Oct 18, 2019.

  1. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    Hi ,
    i can spawn multiple enemy by using random.range, but i don't want to use random , because sometime it spawn same enemy again and again , what should i do ,
    Thank you !
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnerTrigger : MonoBehaviour
    6. {
    7.     public GameObject[] enemyPrefab;
    8.     public Transform[] spawnPoint;
    9.     public int numOfenemy = 4;
    10.  
    11.     BoxCollider boxCollider;
    12.  
    13.     void Start()
    14.     {
    15.         boxCollider = GetComponent<BoxCollider>();
    16.     }
    17.  
    18.  
    19.     void Update()
    20.     {
    21.      
    22.     }
    23.     public void OnTriggerEnter(Collider other)
    24.     {
    25.         if (other.tag == "Player")
    26.         {  for (int startCount=0;startCount<numOfenemy;startCount++)
    27.             {
    28.  
    29.                 StartCoroutine(enemySpawn());
    30.              
    31.             }
    32.             boxCollider.enabled = false;
    33.          
    34.         }
    35.     }
    36.     IEnumerator enemySpawn()
    37.     {
    38.         yield return new WaitForSeconds(Random.Range(1,5) );
    39.         Transform _sp = spawnPoint[Random.Range(0, spawnPoint.Length)];
    40.         GameObject _Ep = enemyPrefab[Random.Range(0,enemyPrefab.Length )];
    41.         Instantiate(_Ep , _sp.position, _sp.rotation);
    42.  
    43.  
    44.    
    45.     }
    46. }
    47.  
     
    Last edited: Oct 22, 2019
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Create a list of all possible values, randomize that, then just iterate through it. It'll assure that you never hit the same number twice until you loop around to the beginning. To your players, it'll appear random.
     
    Joe-Censored likes this.
  3. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    ?
     
    Last edited: Oct 18, 2019
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Well, you already have a list of spawn points in your script. You need is to add one integer to store previous used spawner index and increment it after every spawn. Then you need to shuffle the list in the start method and after all spawners were used. Also reset index to zero after that.
    Suffling is easy thing to implement even for complete beginner. Iterate array of spawners from 0 to the last one. For each spawner generate random index between 0 and spawners count and interchange those two in the array. That's it, array shuffled.
     
    ashishkushwaha likes this.
  5. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    i tried but not getting it :( some how i agree your with your thought ,this type of logic should use, but to do in a ground level , not getting how to do
     
  6. Ankushmaurya

    Ankushmaurya

    Joined:
    Oct 31, 2019
    Posts:
    1
    Hi Ashish try below;

    private int lastNumberSpawn;
    private int lastNumberPrefab;

    IEnumerator enemySpawn()
    {
    yield return new WaitForSeconds(Random.Range(1, 5));
    Transform _sp = spawnPoint[GetRandomForSpawn(0, spawnPoint.Length)];
    GameObject _Ep = enemyPrefab[GetRandomForPrefab(0, enemyPrefab.Length)];
    Instantiate(_Ep, _sp.position, _sp.rotation);
    }

    private int GetRandomForSpawn(int min, int max)
    {
    int rand = Random.Range(min, max);
    while (rand == lastNumberSpawn)
    rand = Random.Range(min, max);
    lastNumberSpawn = rand;
    return rand;
    }

    private int GetRandomForPrefab(int min, int max)
    {
    int rand = Random.Range(min, max);
    while (rand == lastNumberPrefab)
    rand = Random.Range(min, max);
    lastNumberPrefab = rand;
    return rand;
    }
     
    ashishkushwaha likes this.