Prototype: Spawning Enemies

Introduction

We’ve made a lot of progress on the prototype so far, and today we’re going to spawn enemies over time. If you missed the last post in the prototype series, check it out here!

The Challenge

The challenge is to create a spawn manager that will continually spawn enemy instances at a configurable duration in seconds.

Let’s have some fun.

Spawn Manager

First, I’ll create a new script named SpawnManager and I’ll attach it as a component to a new GameObject of the same name.

In the spawn manager script, I’ll add a field named _enemyPrefab and make it configurable via the inspector with the SerializeField attribute. To keep our hierarchy clean, I’ll also create an _enemyContainer field to act as the parent for our enemy prefab instances. I’ll quickly switch to Unity and set these properties in the inspector.

I don’t need the Update method, so I’ll remove it and in it’s place I’ll add a Coroutine method. I will have an upcoming side-quest guide to talk about Coroutines in more detail. For now, just know that this will allow us to continually spawn enemy instances.

For now, we’ll simply spawn a new enemy every 5 seconds. I’ll create several variables that we can adjust during play testing to control the spawn wait interval and random position for the enemies when they are spawned. The coroutine itself is a simple loop that yields for 5 seconds, and then instantiates a new enemy instance at a random horizontal location.

Here are the fields we’ll use: Spawn Manager Fields

And here’s the spawn coroutine: Spawn Manager Coroutine

To start the coroutine, we’ll call StartCoroutine from the Start method.

Start Coroutine

Spawning Enemies Prototype

Summary

The core mechanics of our basic shooter game are practically done! I hope you found this guide to be helpful. Now that we have a spawn manager, it’s time to consider some pickup items we could introduce.

Stay tuned for the next post.
Take care.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s