Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1784
Rep : 15
Join date : 2013-12-18
View user profile

wave spawnig

on Wed Jun 04, 2014 9:46 pm
hey guys do enyone have a script that can spawn enemy in wave it with well really be helpful
avatar
Admin
Admin
Posts : 608
Points : 3170
Rep : 406
Join date : 2013-12-11
Age : 21
View user profilehttps://www.youtube.com/user/misterninjaboy

Re: wave spawnig

on Wed Jun 04, 2014 11:56 pm
Code:
 public GameObject enemy;
 
    private bool waveActive = false;
 
    public Transform[] spawnPointRoot;
 
    private int waveLevel = 0;
    private float diffucultyMultiplier = 1.0f;
    private float intermissionLength = 10f;
    private int enemyCount = 0;
    private ArrayList enemies;
    private bool allEnemiesSpawned = false;
 
    private float velocity = 4f;
    private float health = 20f;
    private int enemyAmount = 10;
    private float spawnIntervall = 2;
 
    private GUIScript gui;
 
    public enum GameState {
      preStart,
      activeWave,
      intermission
    }
 
    GameState state = GameState.preStart;
 
    void Start(){
      enemies = new ArrayList();
      gui = Camera.main.GetComponentInChildren<GUIScript>();
    }
 
    void Update () {
      switch(state){
 
        case GameState.preStart:
          if(gui.startWave){
              setNextWave();
              startNewWave();
              gui.startWave = false;
          }else {
 
          }
        break;
 
        case GameState.activeWave:
          if(enemyCount == 0 && waveActive && allEnemiesSpawned){
              finishWave();
          }
        break;
 
        case GameState.intermission:
        break;
      }
    }
 
    void LateUpdate(){
      for(int i = 0; i < enemies.Count; i++){
        if((GameObject)(enemies[i]) == null){
          enemies.Remove(enemies[i]);
        }
      }
      enemyCount = enemies.Count;
    }
 
    void setNextWave(){
      diffucultyMultiplier = (diffucultyMultiplier * waveLevel) / 2;
    }
 
    void startNewWave(){
      state = GameState.activeWave;
 
      StartCoroutine(StartMission(1.5f));
 
      waveLevel++;
    }
 
    IEnumerator InterMission(float seconds){
      yield return new WaitForSeconds(seconds);
      setNextWave();
      startNewWave();
    }
 
    IEnumerator EnemySpawnerRoutine(float spawnIntervall, int enemyAmount, float velocity, float health){
      for(int i = 0; i < enemyAmount; i++){
        spawnNewEnemy(velocity, health);
        yield return new WaitForSeconds(spawnIntervall);
      }
      allEnemiesSpawned = true;
    }
 
    void finishWave(){
      StartCoroutine("InterMission",intermissionLength);
      state = GameState.intermission;
      waveActive = false;
    }
 
    void spawnNewEnemy(float velocity, float health){
      GameObject e = (GameObject) Instantiate(enemy, new Vector3(0,0,0), Quaternion.identity);
      EnemyScript es = e.GetComponentInChildren<EnemyScript>();
      int i = Random.Range(0,2);
      es.setWaypoints(spawnPointRoot[i]);
      es.maxHealth = health;
      es.currHealth = health;
      es.speed = velocity;
      enemyCount++;
      enemies.Add(e);
    }
 
    IEnumerator StartMission(float seconds){
      yield return new WaitForSeconds(seconds);
 
      allEnemiesSpawned = false;
 
      StartCoroutine(EnemySpawnerRoutine(spawnIntervall,enemyAmount,velocity,health));
 
      waveActive = true;
    }
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 64
Points : 1784
Rep : 15
Join date : 2013-12-18
View user profile

Re: wave spawnig

on Thu Jun 05, 2014 7:51 pm
I got 21 error when i past this code in my game can you look this code over and send it back to me or tell me what i need to do so it can work
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1642
Rep : 19
Join date : 2014-06-14
View user profile

Re: wave spawnig

on Wed Aug 13, 2014 2:43 pm
Its easy to make the script but what kind of game are you making? B/c it depends on the type of the game
Back to top
Permissions in this forum:
You cannot reply to topics in this forum