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?
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!
Building the Explosion
To improve the explosion, you’ll alter the properties of one of the particle system’s modules. Can you guess which module to alter? Here’s a hint — you’ve already used it.
If you guessed the Emission module, give yourself a pat on the back!
Expand the Emission Module. Rate is the number of particles spawned per second. For this explosion, you won’t want a steady flow of particles, but rather a sudden burst.
Set the Rate over Time to 0. Now look beneath the Rate over Distance, and you’ll see a list of Bursts that’s empty by default:
A Burst is a collection of particles emitted all at once at a particular point in time.
Click on the + button at the bottom right to add a new Burst. You’ll see two fields: Time and Count:
Leave the Time at 0, and set Count to 150. These settings will make the particle system emit 150 particles all at once at the start of the system.
Play your scene; how do things look now?
Now that looks more like an explosion! While this explosion looks better, the shape is still an awkward cone and the particles don’t fade out — they simply disappear. You’ll need to mold your explosion to give it that final touch.
To get started, expand the Shape Module:
You’ve already used this module for the torch’s fire shape, but there are several more shapes to choose from. Click on the dropdown box that says Cone to see all options available to you:
Each shape affects the emitter in a different way. Each animation below shows the same emitter, with only the shape changed:
Sphere
HemiSphere
Cone
Box
Mesh (Cube)
Circle
Edge
You can get many different effects from the same system — simply by changing the shape! To create a realistic explosion, set the shape to Sphere.
Run the scene and prepare to be blown away:
Now that looks awesome!
While the explosion looks good, there’s one small problem. The particles simply disappear. This is a jarring effect and doesn’t look natural at all. Rather than just disappear, the particles should fade over time to make the explosion fade away.