Implementing spawning
We have created lots of objects in the Editor that define our level, but once the game begins, and according to the Player actions, new objects must be created to better fit the scenarios generated by Player interaction. Enemies might need to appear after a while, or bullets must be created according to the player input; even when enemies die, there's a chance of spawning some power-up. This means that we cannot create all the needed objects beforehand but should create them dynamically, and that's done through scripting.
In this section, we will examine the following spawning concepts:
- Spawning Objects
- Timing actions
- Destroying Objects
We will start by looking at the Unity Instantiate
function, which allows us to create instances of Prefabs at runtime, such as when pressing a key, or in a time-based fashion, such as making our enemy spawn bullets after a certain amount of time. Also, we will learn how to destroy these Objects...