Introduction to Unity: Particle Systems

Unity’s particle system is both robust and feature packed. In this tutorial, you’ll learn the ins-and-outs of it to create both fire and explosions. By Anthony Uccello.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 3 of this article. Click here to view the first page.

Introducing the Emission Module

The Emission module handles the number and timing of emitted particles in the system to create anything from a continuous flow, to a sudden burst of particles, depending on your needs.

While still in the particle system’s Inspector, click on the Emission module title:

Emission module title in the particle system's Inspector

This opens up the Emission module:

Emission module in the particle system's Inspector

Rate over Time represents the number of particles emitted per second. Set Rate over Time to 15.

Run your scene again; your particle system looks a little more like a burning flame now.

Torch with light that looks a lot like smoke... closer to flame

Admittedly, it still looks like smoke. But here comes the biggest change in this tutorial so far: a custom texture!

Adding a Custom Texture

All particles have a particle material and a texture that defines how they look. There’s only so much you can do with the default texture. By changing the texture, you can create effects like magic stars, smoke and of course, fire.

Changing the particle texture is quite easy. Up to this point, the particles have been drawn on the screen using the Default-Particle material, which is a particle material with a circular gradient texture:

Default-Particle setting

To change the material, select the TorchFireParticles GameObject inside the Hierarchy. Then find the the particle system component in the Inspector and open up the particle system’s Renderer module.

Open the Materials folder in the Project View, and drag the FireMaterial Material to the Material property:

FireMaterial Material in the Material property

Finally, run the scene to see your custom texture in use:

Torch with light that has the right color for flame

Can you feel the heat? The flame’s a bit too wide though; to fix that, you’ll have to change the shape of the particle system.

Changing the Particle System’s Shape

The Shape module, as the name implies, controls the shape and the behavior of particles in that shape. You can choose from several different shapes; each has their own particular settings. This lets you create particles in a box, a sphere, or even in your own custom mesh!

Expand the Shape module in the Inspector:

Shape module in the Inspector

The shape of your particle system is set to cone, which means particles emit from the base and move outwards at an angle:

How articles move in a cone shape

In the example above, the base is colored blue, the angle is green and the particles are red. Also notice that while you have the Shape Module expanded, you’re presented with a handy preview of the cone in the Scene view:

Cone preview

Changing the Angle changes the size of the cone to make it wider or narrower. Set this to 7; you’ll get a nice tight flame that widens slightly as the particles rise.

Changing the Radius changes the size of the base. The larger this value is, the more the particles will scatter as they emit. Set this value to 0.2; this ensures the flames will start inside the fuel holder of the torch.

Run your scene and see how the shape of the flame has changed:

Torch with a much-improved flame shape

That’s starting to look like a real flame! The finishing touch is to change the size of the particles over the course of their lifetime.

Changing Size Over Lifetime

With the Size over Lifetime module, you can create particles that grow or shrink during their lifetime, or even pulsate like fireflies in a forest.

In your particle system’s module list, look for Size over Lifetime. It’s not enabled by default, so tick on the checkbox next to the module name to enable it:

checkbox next to the Size over Lifetime checkbox/>

Expand the Size over Lifetime module by clicking on its name. This will display a dark gray background with a flat Curve running along the top:

How Size over Lifetime module looks when it's expanded

Click on the dark gray background to open the curves editor at the bottom of the Inspector. The horizontal axis depicts the lifetime of the particle, while the vertical axis depicts its size:

You can move the keys at either end of the red line to edit the curve; you can also add additional keys by double-clicking anywhere on the curve. To delete keys, right-click on the key to remove and select Delete Key. You can also select one of the preset curves at the bottom:

Grid with a straight, 45-degree line between size and lifetime

If you think about flames in the real world, they tend to shrink as the particles rise. To mimic this, select the third preset from the right to create a downward-facing slope:

Variations in the line between size and lifetime

Run your scene to see your effect in all its fiery glory!

Torch with a realistic-looking flame

Congratulations! You’ve learned how to set up a new particle system and bend it to your will to make a beautiful fire effect.

In the next section, you’ll let your inner Michael Bay shine as you learn to create explosion effects!

Building a Bomb (Effect)

Making an exploding effect is quite simple in Unity. Once you know how to instantiate particles at will, you can use this effect for things such as car wheels sparking when they scrape the ground, or balloons that pop and shower confetti.

Open up the Bomb scene from the Project Window and play the scene:

empty bomb scene

There’s a floor at the bottom of the scene, but apart from that, there’s not much going on.

To spawn bombs, drag the Bomb Prefab to the Bomb Emitter prefab slot:

Correct settings in the DragBombPrefabToEmitter

Play the scene again to see your bombs appear:

bombs droping from ceiling and falling through floor

The emitter creates a new bomb every two seconds. To put a neat spin on things, you’ll add some rotational force to the bomb when it spawns.

Open up the Bomb script inside the Scripts folder in your Project Window.

Add the following code to Start():

void Start()
{
    float randomX = UnityEngine.Random.Range (10f, 100f);
    float randomY = UnityEngine.Random.Range (10f, 100f);
    float randomZ = UnityEngine.Random.Range (10f, 100f);

    Rigidbody bomb = GetComponent<Rigidbody> ();
    bomb.AddTorque (randomX, randomY, randomZ);
}

The first three lines generate random float values between 10 and 100 for the x, y and z axes. Next, you get a reference to the bomb’s Rigidbody component and apply torque to it. This causes the bomb to rotate in a random direction. Save your script changes, return to Unity and run the scene.

Bombs spinning as they fall

The bombs now rotate nicely while they fall — but you were promised explosions!

In the Hierarchy, press the Create button and select Create Empty. Click on the newly created GameObject and name it ExplosionParticles. Next, add a new particle system to the GameObject. If you’ve forgotten how to create a particle system, scroll on up for a refresher.

With your particle system in place, drag the ExplosionParticles GameObject from the Hierarchy to the Prefabs folder in the Project Browser. After that’s done, delete the ExplosionParticles GameObject from the Project Hierchy.

How to delete the ExplosionParticles GameObject from the Hierarchy

Next, select the Bomb Prefab inside the Prefabs folder and drag the ExplosionParticles Prefab to the Bomb‘s Explosion Particles Prefab slot like so:

How to drag the ExplosionParticles Prefab to the Bomb's Explosion Particles Prefab slot

Now, a new Explosion Particles GameObject will spawn when a bomb touches the ground.

Play your scene to see how the explosion looks. If you’re experiencing the pink textures bug, don’t worry, you’re about to change the texture.

Explosion particles appearing when the bomb reaches the floor

Very…uh…magical, but nothing near an explosion yet!

As with the torch, you’ll be using the Fire material for the particle system.

Select the ExplosionParticles Prefab in the Project Window, then expand the Renderer Module in the Inspector. Drag the FireMaterial from the Materials folder in the Project Window to the Material slot as shown below:

How to drag the FireMaterial to the Material slot

To complete the effect, you’ll have to modify the following settings in the Main module:

Where to modify the Main module's settings

  1. Set the Duration to 0.70.
  2. Looping should be disabled. The particles should emit just once.
  3. Set the Start Lifetime to 0.7.
  4. Set the Start Speed to 10.
  5. Set Start Size to 2.
  6. Set the Gravity Modifier to 1. This will make the particles drop slightly at the end.

Run your bomb scene to see what you’ve built:

Red fire-like particles emitting when the bomb reaches the floor

Well, it’s kind of explod-ish, but you can definitely do better!