Now that we have the basis for our game, let's spend some time to make the project look nicer. Particle systems are one of my go-to things to add juiciness to a game project and helps to set your project apart from others. Particle systems are composed of two separate parts: a particle and the thing that emits it (called an emitter). A particle is a small object that stores properties; generally, we try to make these objects as simple as possible, as we want to spawn a large number of them. The emitter's job is to spawn a number of these and initialize their properties. Thankfully, Unity has a fully featured particle editor that's included with the engine, and we're going to use it in this section. Perform the following steps:
Once you do this, you should see a default particle system show up. Do note that the system will only animate if it is the object selected and Unity is the active window.
Explosion
. First, under the Particle System tab, change Duration to 1.00
.0
and 1
. Do the same with Start Speed. Make Start Size use random values between 0
and .5
.210
, 224
, 230
) and an Alpha value of 125
. Have a look at the following screenshot:200
. This is how many particles are spawned at a time..35
to fit the rim of the ship. Enable the Random Direction option.Prefabs
folder. After that, delete the Explosion
object in your Hierarchy object.EnemyBehaviour
script file. We will first want to add in a new variable for us to use to spawn this explosion when it dies:// When the enemy dies, we play an explosion public Transform explosion;
EnemyBehaviour
script, let's spawn an explosion whenever we die. Inside your if(health <= 0)
section of OnCollisionEnter2D
, add in the following lines:// Check if explosion was set if(explosion) { GameObject exploder = ((Transform)Instantiate(explosion, this.transform.position, this.transform.rotation)).gameObject; Destroy(exploder, 2.0f); }
As you can see here, we use the Instantiate function in order to create an explosion and we call the Destroy function to have it automatically delete itself (the first parameter) after 2 seconds as passed (the second parameter).
And now, whenever an enemy dies, it will spawn an explosion for us to see!