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 Game Development Blueprints

You're reading from   Unity Game Development Blueprints Explore the various enticing features of Unity and learn how to develop awesome games

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher
ISBN-13 9781783553655
Length 318 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John P. Doran John P. Doran
Author Profile Icon John P. Doran
John P. Doran
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. 2D Twin-stick Shooter 2. Creating GUIs FREE CHAPTER 3. Side-scrolling Platformer 4. First Person Shooter Part 1 – Creating Exterior Environments 5. First Person Shooter Part 2 – Creating Interior Environments 6. First Person Shooter Part 3 – Implementing Gameplay and AI 7. Creating Save Files in Unity 8. Finishing Touches 9. Creating GUIs Part 2 – Unity's New GUI System Index

Particle systems for enemy explosion

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. Particles systems are composed of two separate parts: a particle and the thing that emits it. 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:

  1. Create a new particle system by going to GameObject | Create Other | Particle System. Have a look at the following screenshot:
    Particle systems for enemy explosion

    Note

    If you are using Unity 4.6 use GameObject | Particle System to create the particle system.

    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.

  2. Change the object's name to Explosion. First, under the Particle System tab, change Duration to 1.00, and then uncheck Looping.
  3. Click on the downwards-facing arrow on the right-hand side of Start Lifetime, and change the values to be Random Between Two Constants. Change those values to 0 and 1. Do the same with Start Speed. Make Start Size use random values between 0 and .5.
  4. Next, we will set the object's Start Color value to the same color as the UFO ship (you can use the eyedropper tool or set it to 210, 224, 230) and an Alpha value of 125. Have a look at the following screenshot:
    Particle systems for enemy explosion
  5. Open the Emission tab, and change Rate to 200. This is how many particles are spawned at a time.
  6. Open the Shape tab, change the Shape property to Sphere, and then set Radius to .35 to fit the rim of the ship. Enable the Random Direction option.
  7. Back in the Explosion section, change the Simulation Space to World; that way, if this object moves, the already-spawned particles will not move.
  8. Now, make this object a prefab by dragging-and-dropping it into the Prefabs folder. After that, delete the Explosion object in your Hierarchy object.
  9. Go back to your 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;
  10. Back in Inspector, drag-and-drop your new explosion prefab to fill in the explosion variable slot in our enemy prefab.
  11. Coming back to the EnemyBehaviour script, let's spawn an explosion whenever we die. Inside your if(health <= 0) section of CollisionEnter2D, 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);
    }
  12. Save your script and scene files, and run your project! Have a look at the following screenshot:
    Particle systems for enemy explosion

And now, whenever an enemy dies, it will spawn an explosion for us to see!

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