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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Introduction to Unity: Particle Systems
25 mins
- Getting Started with Particle Systems
- Adding a Particle System
- A Closer Look at a Particle System
- More Main Module Properties
- Introducing the Emission Module
- Adding a Custom Texture
- Changing the Particle System’s Shape
- Changing Size Over Lifetime
- Building a Bomb (Effect)
- Building the Explosion
- Changing Color
- Where to Go From Here?
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:
This opens up the Emission module:
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.
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:
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:
Finally, run the scene to see your custom texture in use:
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:
The shape of your particle system is set to cone, which means particles emit from the base and move outwards at an angle:
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:
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:
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:
/>
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:
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:
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:
Run your scene to see your effect in all its fiery glory!
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:
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:
Play the scene again to see your bombs appear:
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.
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.
Next, select the Bomb Prefab inside the Prefabs folder and drag the ExplosionParticles Prefab to the Bomb‘s Explosion Particles Prefab slot like so:
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.
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:
To complete the effect, you’ll have to modify the following settings in the Main module:
- Set the Duration to 0.70.
- Looping should be disabled. The particles should emit just once.
- Set the Start Lifetime to 0.7.
- Set the Start Speed to 10.
- Set Start Size to 2.
- 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:
Well, it’s kind of explod-ish, but you can definitely do better!