Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 5.x 2D Game Development Blueprints

You're reading from   Unity 5.x 2D Game Development Blueprints Explore the features of Unity 5 for 2D game development by building three amazing game projects

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781784393106
Length 252 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Francesco Sapio Francesco Sapio
Author Profile Icon Francesco Sapio
Francesco Sapio
Abdelrahman Elsayegh Abdelrahman Elsayegh
Author Profile Icon Abdelrahman Elsayegh
Abdelrahman Elsayegh
Arrow right icon
View More author details
Toc

Creating an enemy spawner


The next thing to do is to actually spawn enemies against the player. This can be done in many different ways. Here, we are going to see a simple way to do that.

The key concept is that we have a coroutine that gradually spawns enemies.

Tip

If you are not familiar with coroutines, you can check out the following documentation: http://docs.unity3d.com/Manual/Coroutines.html.

Let's get started with opening the EnemyScript and adding a new variable that holds the position where we want to spawn the enemies. Since we want to set this in the Inspector, we need to set it to public:

public Vector3 SpawnPoint; 

Furthermore, we need one more variable, public again, that stores the prefab for the enemy in order to spawn all the enemies:

public GameObject enemyPrefab; 

Also, we need some other variables that we are going to use to set how many enemies the coroutine is going to spawn and how fast:

public int numberOfEnemiesToSpawn = 50; 
public float minSpawnTime =...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime