Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
I am currently trying to make a sort of io version of call of duty zombies and dying light. So far it is coming along nicely, until i hit this snag.
I would like to make 2 modes: 1) an open world where quests can be completed *kinda like dying light ish*
2) a survival map based thing like COD: zombies where you can buy perks and guns off walls and use the mystery box
I am trying to make the waves in which zombies spawn infinitely and am also trying to make them increase in difficulty (that being the number of enemies in each wave increasing by 3 (starting at 4 for round 1) until the player reaches wave 50 where the number of zombies will be the same each wave until the player dies. AND the rate at which they spawn, decreasing by 0.01 of a second from 1 second between each spawn to 0.5 seconds per spawn. which will end at round 50 and be 0.5 seconds from there).

I have currently gotten so called "rounds" to work in a way where they have to be manually set by me before starting the game (where i also have to specify the amount of enemies for each wave and the time in between each spawn in seconds). I also have two different types of enemies. a "jogger" and a "brute." I want to specify in the code the rounds (like i have) that the brute spawns in on and then say that the joggers spawn every other round. Here is the code i currently have:

Any help is greatly appreciated. p.s. im not getting any errors :)
Thanks heaps. - ADS

What I have tried:

C#
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Spawner : MonoBehaviour
{    
    public Wave[] waves;
    public Jogger jogger;
    public Brute brute;
    
    Wave currentWave;
    int currentWaveNumber;

    int enemiesRemainingToSpawn;
    int enemiesRemainingAlive;
    float nextSpawnTime;    

    //text wave number
    public Text currentWaveDisplay;
    bool waveComplete;

    //text next wave countdown timer ****
    public Text countdownText;
    public int timeLeft = 3;

    //enemies remaining display
    public Text enemiesRemaining;

    void Start()
    {
        NextWave();
        currentWaveNumber = 1;
        //timer
        StartCoroutine("LoseTime");

    }

    void Update()
    {
        //timer display
        countdownText.text = "Wave Starting In: " + timeLeft;
        if (timeLeft <= 0)
        {
            StopCoroutine("LoseTime");
            countdownText.text = "Wave has begun!";
        }

        if (timeLeft <= 0)
        {

            //rounds and special brute rounds
            if (enemiesRemainingToSpawn > 0 && Time.time > nextSpawnTime)
            {
                if (currentWaveNumber < 6)
                {
                    enemiesRemainingToSpawn--;
                    nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;

                    Jogger spawnedEnemy = Instantiate(jogger, Vector3.zero, Quaternion.identity) as Jogger;
                    spawnedEnemy.OnDeath += OnEnemyDeath;
                }
                if (currentWaveNumber == 6)
                {
                    enemiesRemainingToSpawn--;
                    nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;

                    Brute spawnedEnemy = Instantiate(brute, Vector3.zero, Quaternion.identity) as Brute;
                    spawnedEnemy.OnDeath += OnEnemyDeath;
                }
                if (currentWaveNumber > 6 && currentWaveNumber < 11)
                {
                    enemiesRemainingToSpawn--;
                    nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;

                    Jogger spawnedEnemy = Instantiate(jogger, Vector3.zero, Quaternion.identity) as Jogger;
                    spawnedEnemy.OnDeath += OnEnemyDeath;
                }
                if (currentWaveNumber == 11)
                {
                    enemiesRemainingToSpawn--;
                    nextSpawnTime = Time.time + currentWave.timeBetweenSpawns;

                    Brute spawnedEnemy = Instantiate(brute, Vector3.zero, Quaternion.identity) as Brute;
                    spawnedEnemy.OnDeath += OnEnemyDeath;

                }
                
            }
            
        }
        if (currentWaveNumber == 1)
        {
            currentWaveDisplay.text = "I"; 

            waveComplete = false;
        }
        if (waveComplete = true && currentWaveNumber == 2)
        {
            currentWaveDisplay.text = "II";

            waveComplete = false;
        }
        if (waveComplete = true && currentWaveNumber == 3)
        {
            currentWaveDisplay.text = "III";

            waveComplete = false;
        }
        if (waveComplete = true && currentWaveNumber == 4)
        {
            currentWaveDisplay.text = "IV";

            waveComplete = false;
        }
        if (waveComplete = true && currentWaveNumber == 5)
        {
            currentWaveDisplay.text = "V";

            waveComplete = false;
        }
        if (waveComplete = true && currentWaveNumber == 6)
        {
            currentWaveDisplay.text = "VI";

            waveComplete = false;
        }
        if (waveComplete = true && currentWaveNumber == 7)
        {
            currentWaveDisplay.text = "VII";

            waveComplete = false;
        }

        //enemies remaining display
        enemiesRemaining.text = "Enemies Remaining: " + enemiesRemainingAlive;
       
    }

        
    //timer delay
    IEnumerator LoseTime ()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }
    }

    void OnEnemyDeath()
    {
        enemiesRemainingAlive--;

        if (enemiesRemainingAlive == 0)
        {
            waveComplete = true;
            timeLeft = 3;
            StartCoroutine("LoseTime");
            
            NextWave();
            
        }
    }

    void NextWave()
    {

        currentWaveNumber++;
        //fix auto change of number of enemies
        //fix auto change of timebetweenspawns
        print("Wave: " + currentWaveNumber);
        if (currentWaveNumber - 1 < waves.Length)
        {
            currentWave = waves[currentWaveNumber - 1];

            enemiesRemainingToSpawn = currentWave.enemyCount;
            enemiesRemainingAlive = enemiesRemainingToSpawn;
        }
    }

    [System.Serializable]
    public class Wave
    {
        public int enemyCount = 4;
        public float timeBetweenSpawns = 1;
        
    }

}
Posted
Updated 1-Apr-18 22:06pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900