Zombie Swipe: Spawning Enemies in Unity – Iteration 3

I find that, as I learn my way around Unity and C#, I have a lot of ideas on how to problem solve and achieve my goals but not a lot of concrete knowledge on how to apply those ideas. I do not have a firm grasp on C# myriad of tools within its toolbox or even a strong sense of its syntax. But I am learning as I go and doing is something that makes learning fun for me.

For Zombie Swipe (working title) I wanted to create an enemy spawn system that would:

  • Randomly position zombie enemies outside of the camera’s view
  • Pull from a pool of zombie types
  • Increase in difficulty after so many waves of enemies

My first version of this was it in its most basic form. Spawn zombies somewhere. Anywhere. I don’t care! Just make zombies appear.

My second version was to get zombies to spawn off screen, outside of the camera’s view, so it appears like zombies are rushing into the scene.

Now, on my third version, I have set up a pool of three different zombie types to pull from and a rudimentary way to increase difficulty. It’s awfully messy, though. I am both proud and horrified of the monster I have created.

What we have below is a method that, essentially, repeats itself to spawn zombies at different difficulty levels. If the wave count is less than 10 it spawn only the WeakZombies – which is 0 in the List called zombies. If the wave count is greater than 10 but less than or equal to 19 it will spawn both WeakZombies and FastZombies – which are 0 and 1 in the zombies List. And finally, when the wave count exceeds 19 it will spawn all three types of zombies. Weak, Fast, and Strong.


IEnumerator SpawnWaves()
{
while (Player.isDead == false && ScoreCntl.wave <= 10)
{
yield return new WaitForSeconds(startWait);
ScoreCntl.wave += 1;

for (int i = 0; i < zombieCount; i++)
{
Vector3 spawnPosition = spawnPoints[Random.Range(0, spawnPoints.Count)];
Quaternion spawnRotation = Quaternion.identity;
GameObject created = Instantiate(zombies[0], spawnPosition, spawnRotation);
created.transform.SetParent(canvas.transform.GetChild(5));
created.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

yield return new WaitForSeconds(spawnWait);
}

yield return new WaitForSeconds(waveWait);
}

while (Player.isDead == false && (ScoreCntl.wave > 10 && ScoreCntl.wave <= 19))
{
yield return new WaitForSeconds(startWait);
ScoreCntl.wave += 1;

for (int i = 0; i < zombieCount; i++)
{
Vector3 spawnPosition = spawnPoints[Random.Range(0, spawnPoints.Count)];
Quaternion spawnRotation = Quaternion.identity;
GameObject created = Instantiate(zombies[Random.Range(0, 2)], spawnPosition, spawnRotation);
created.transform.SetParent(canvas.transform.GetChild(5));
created.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

yield return new WaitForSeconds(spawnWait);
}

yield return new WaitForSeconds(waveWait);
}

while (Player.isDead == false && ScoreCntl.wave > 19)
{
yield return new WaitForSeconds(startWait);
ScoreCntl.wave += 1;

for (int i = 0; i < zombieCount; i++)
{
Vector3 spawnPosition = spawnPoints[Random.Range(0, spawnPoints.Count)];
Quaternion spawnRotation = Quaternion.identity;
GameObject created = Instantiate(zombies[Random.Range(0, zombies.Count)], spawnPosition, spawnRotation);
created.transform.SetParent(canvas.transform.GetChild(5));
created.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);

yield return new WaitForSeconds(spawnWait);
}

yield return new WaitForSeconds(waveWait);
}
}

Not only is this clunky and gross, but it doesn’t leave me room to grow. For a game that has infinite waves I want to be able to up the difficulty every so many waves and even throw in some random scenarios at certain waves. Say, if you reach wave 50, it may end up being 100 of the FastZombies. I’ll need to work on this to make it robust and eliminate some of the repeating code (which I understand is a no-no). I look forward to Iteration 4 being cleaner, but for now it will have to do.

Tidbits related to the spawn system:


void Start()
{
deathUI.SetActive(false);
zombieCount = 5;

PopulateZombies();

StartCoroutine(SpawnWaves());

}

And how I randomly generate the zombies outside of the camera:


void PopulateZombies()
{
//Populate the types of Zombies in a list
zombies.Add(zombieWeak);
zombies.Add(zombieFast);
zombies.Add(zombieStrong);

//Populate the list of Spawn Points with random points within specified ranges.
//This will cause the Zombies to spawn outside of the camera view from all sides
spawnPoints.Add(new Vector3(Random.Range(-10f, 10f), 7f, 1f)); //Top
spawnPoints.Add(new Vector3(Random.Range(-10f, 10f), -7f, 1f)); //Bottom

spawnPoints.Add(new Vector3(10f, Random.Range(-6f, 6f), 1f)); //Right
spawnPoints.Add(new Vector3(10f, Random.Range(-2f, 2f), 1f)); //Right Middle

spawnPoints.Add(new Vector3(Random.Range(-6f, 10f), -9f, 1f)); //??
spawnPoints.Add(new Vector3(Random.Range(-4f, 11f), -9f, 1f)); //??

spawnPoints.Add(new Vector3(-10f, Random.Range(-6f, 6f), 1f)); //Left
spawnPoints.Add(new Vector3(-10f, Random.Range(-2f, 2f), 1f)); //Left Middle

spawnPoints.Add(new Vector3(Random.Range(-2f, 2f), 8f, 1f)); //Top Middle
spawnPoints.Add(new Vector3(Random.Range(-4f, 4f), -8f, 1f)); //Bottom Middle

//Four corners
spawnPoints.Add(new Vector3(10f, 6f, 1f));
spawnPoints.Add(new Vector3(-10f, -6f, 1f));
spawnPoints.Add(new Vector3(10f, -6f, 1f));
spawnPoints.Add(new Vector3(-10f, 6f, 1f));
}

 

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