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

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) »  wave spawnig

wave spawnig

View previous topic View next topic Go down  Message [Page 1 of 1]

1 wave spawnig on Wed Jun 04, 2014 9:46 pm

ryan2brian


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
hey guys do enyone have a script that can spawn enemy in wave it with well really be helpful

View user profile

2 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;
    }

View user profile http://www.youtube.com/user/misterninjaboy

3 Re: wave spawnig on Thu Jun 05, 2014 7:51 pm

ryan2brian


{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
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

View user profile

4 Re: wave spawnig on Wed Aug 13, 2014 2:43 pm

Ninjaboy follower


Member [Lv. 3]
Member [Lv. 3]
Its easy to make the script but what kind of game are you making? B/c it depends on the type of the game

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum