Zombie Swipe: Spawning Enemies in Unity – Iteration 4

Awhile back I shared some hideous code for spawning Zombies in Zombie Swipe. It did what I needed it to do, but I wasn’t thrilled with the structure of the code and knew it was a mess.

I discussed it with a programming friend and he gave me some pointers on how to clean it, including an outline of the code I would need to spawn enemies at certain intervals without using Coroutine. I had spent more time than I care to admit trying to get Zombies to spawn using a combination of While and Ifs and various timer variables.

First things first, I stopped using a list of Vector3 points to spawn Zombies. Instead, I switched to using a SpawnPoints game object and filled it with empty game objects all around the screen. In the code, spawnPosition is then determined by randomly picking one of the SpawnPoints’ children. Compared to my previous code, it is super clean and removes many unnecessary lines.

The code that actually spawns the Zombies much more robust and should allow me, with some fiddling, to spawn more custom scenarios and scale difficulty as the waves increase.

From the ZombieSpawner.cs:

    [SerializeField]
    private Transform spawnPoints; //The parent object containing all of the spawn point objects

    [HideInInspector]
    static public int _currentZombiesAlive = 0; //This decrements each time a Zombie dies. 

    private int _waveZombieCount = 3; //This should be determined by each wave.
    private int _currentWaveZombieCount = 0; //Increments everytime a zombie spawns.
    private float _zombieSpawnDelay = 0.5f; //This should be determined by each wave.
    private float _timeSinceLastZombieSpawn = 0.0f;

    void Start()
    {
        PopulateZombies();
    }

    void Update()
    {
        //Determine if the time since last zombie spawn has surpassed the delay
        //and if the quota of zombies for the current wave has been reached.
        if (Time.time >= _timeSinceLastZombieSpawn + _zombieSpawnDelay
                  && _currentWaveZombieCount < _waveZombieCount)         {             _currentWaveZombieCount += 1;             _currentZombiesAlive += 1;             _timeSinceLastZombieSpawn = Time.time;             if (_currentWaveZombieCount % 3 == 0 && ScoreCntl.wave >= 2)
            {
                //Spawn fast zombie.
                Vector3 spawnPosition = spawnPoints.GetChild(Random.Range(0, spawnPoints.childCount)).position;
                Quaternion spawnRotation = Quaternion.identity;
                GameObject created = Instantiate(zombies[1], spawnPosition, spawnRotation);
                TransformCreated(created);
            }
            else if (_currentWaveZombieCount % 5 == 0 && ScoreCntl.wave > 6)
            {
                //Spawn strong zombie.
                Vector3 spawnPosition = spawnPoints.GetChild(Random.Range(0, spawnPoints.childCount)).position;
                Quaternion spawnRotation = Quaternion.identity;
                GameObject created = Instantiate(zombies[2], spawnPosition, spawnRotation);
                TransformCreated(created);

            }
            else
            {
                //Spawn normal zombie.
                Vector3 spawnPosition = spawnPoints.GetChild(Random.Range(0, spawnPoints.childCount)).position;
                Quaternion spawnRotation = Quaternion.identity;
                GameObject created = Instantiate(zombies[0], spawnPosition, spawnRotation);
                TransformCreated(created);
            }
        }

        //Determine if the quota of zombies has been reached for current wave
        //and if all zombies are dead.
        else if (_currentWaveZombieCount >= _waveZombieCount && _currentZombiesAlive == 0)
        {
            //Get next wave information and reset wave variables.

            _currentWaveZombieCount = 0;  //Keep track of how many Zombies have spawned this wave
            _waveZombieCount = 3;         //How many Zombies to spawn per Wave
            _zombieSpawnDelay = 0.5f;     //How long to wait between each Zombie spawn
            ScoreCntl.wave += 1;          //Increment the Wave count
        }
    }

I have learned quite a bit from the outline my friend provided and filling in the functionality. Hopefully I learned enough to be able to create this sort of functionality again should the need arise.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s