Home / Legacy / Lesson 5: Particle Systems and Audio

Lesson 5: Particle Systems and Audio

/
/
/
612 Views
This entry is part 7 of 15 in the series Unity 3d Fundamentals Course

In this lesson we are going to learn how to create a Particle System for our AK-47 muzzle flash and also add sound effects.

Lesson Outline

Creating the Muzzle Flash

In the previous lesson we used raycasting to make our weapon being able to shoot. But we also need to add some special effects to show the player that something is really happening. We will create a Particle System for weapon muzzle flash effect.

What is a Particle System?

Particle Systems control the behaviour of dynamic entities in our game, such as flames, smoke, liquids, among others. They simulate these effects by emitting from dozens to thousands of sprites or meshes giving the illusion of a complete individual entity. We have total control over our particle systems. It is possible to customize the behaviour of the system as a whole and also over the properties of each particle that is emitted, allowing us to reach almost any desired effect.

…and back to the Muzzle Flash

First we will create a root object to hold all the weapon effects and place it as a child of the weapon.

Lesson 5: Particle Systems and Audio

Next we need to create a new Particle System and add it as a child of the root object.

Lesson 5: Particle Systems and Audio

The particle system is by default facing upwards with a cone shape. In order to see the system shape, expand the property on the inspector. We need to adjust its rotation to emit the particles in the gun direction.

Lesson 5: Particle Systems and Audio

The system shape defines the volume to create and emit the particles. This property defines the overall form of our system. The cone shape is the most adequate for the muzzle effect. However, we need to adjust its radius and angle to emit the particles in a more cylindrical shape.

Lesson 5: Particle Systems and Audio

A new system always emits a default particle. We need to change the appearance of our particles to something more similar to fire. Import the default Particle System package.

Lesson 5: Particle Systems and Audio

Next expand the Renderer property on the particle inspector and change the Material to the FlameD.

Lesson 5: Particle Systems and Audio

By default the system is configured to be constantly emitting particles. We can control how many particles and how they are emitted using the Emission property:

  • Rate: constant emission of particles
    • Time: how many particles to emit per second;
    • Distance: how many particles to emit per unit moved;
  • Bursts: one-shot emission of particles
    • Time: when to emit the burst on the system lifetime;
    • Particles: number of particles to emit;

For the muzzle flash we don’t need a continuous system but rather a one-shot emission that will be enabled each time the player shoots his weapon. So change the emission rate to 0 and create a burst of 15 particles at the beginning of the system.

Lesson 5: Particle Systems and Audio

Use the Particle Effect menu on the Editor Window to control the preview of the system. Play, Stop, change the playback speed or scrub the playback timeline using the available controls.

Lesson 5: Particle Systems and Audio

On the first section of the inspector window we can customize the global variables of the system. We will adjust the Start Lifetime, Start Speed and Start Size. All these variables define the initial values of the particles.

  • Start Lifetime: defines how much time each particle lasts;
  • Start Speed: defines how many units per second the particle moves (defines the length of the shape; a particle will take one second to reach the end of the shape);
  • Start Size: defines the initial unit size of the particles;

We should make the particles a little smaller and decrease their lifetime. We don’t want the particles to last for 1 second because this is a very quick effect and it should appear and disappear instantly, so 0.2 seems about right.

Lesson 5: Particle Systems and Audio

Disable the Looping mode on the first particle system.

Lesson 5: Particle Systems and Audio

Now, our particles appear and disappear almost instantly. But we are going to change their opacity to create a fade effect. The property Colour over Lifetime defines how the colour and opacity change during the particles lifetime.

Lesson 5: Particle Systems and Audio

To customize the colour behaviour we need to use the Gradient Editor. The bar represents the particle lifetime, starting from the left – 0 seconds – to the right – Particle Lifetime Value. On the top we can customize the alpha values and on the bottom we can choose the colours. We may also add new positions along the bar to customize values on any time during the Lifetime – double-click on the required position.

I this case I want my particles to start and finish completely transparent and fade into full opacity somewhere before the middle.

Lesson 5: Particle Systems and Audio

I also want the particle size to behave in the same way as the transparency. The particles should start and end really small and reach the maximum size somewhere in the middle. We can use the Size over Lifetime property to achieve this.

Lesson 5: Particle Systems and Audio

The Size over Lifetime is controlled by a curve. We have several curves available but for the effect we want we need to create a custom one.

Lesson 5: Particle Systems and Audio

Let’s add a new particle system to complete the effect. This second effect is something like a dust cloud that remains at the end of the weapon.

Lesson 5: Particle Systems and Audio

Here you have the full configuration for the second particle system. Add this second system as a child of the main particle system so that they are controlled together.

Lesson 5: Particle Systems and Audio

To finish the muzzle flash add a Point Light to the root object. Change the colour and the range to light the end of the weapon.

Lesson 5: Particle Systems and Audio

Lesson 5: Particle Systems and Audio

Now that we have our visual effects ready they should be enabled only when the weapon is firing.  Disable the Play on Awake of the parent particle system otherwise it will play automatically.

Lesson 5: Particle Systems and Audio

 

Enable the particles

Create a WeaponParticlesController.cs and add public references to the particle system and the point light.

Create a public method and add the code to make the system emit the particles.

To control the light we will toggle the component automatically using the enabled state of the particle system.

Add this script to the root object and assign the necessary references.

Lesson 5: Particle Systems and Audio

Add a public reference to this controller on the Weapon script.

And call the method when the player presses the button to fire the weapon.

Assign the particle controller reference to the weapon script.

Lesson 5: Particle Systems and Audio

Adding Sound Effects

To complete the effect we will add the weapon firing sound.

In this lesson we will just cover the basics of working with sounds. later we will talk about other properties such as 2d vs 3d sounds, compression, looping, etc.

AudioSource

Component required for emitting sounds.

AudioListener

Component required for listening to the sounds emitted by the sources.

There can be only one AudioListener in our scene and it is usually assigned to the Main Camera.

Lesson 5: Particle Systems and Audio

Let’s start by adding an AudioSource to our weapon.

Lesson 5: Particle Systems and Audio

Download the sound we used in the demo and import it to the project.

Download here: Gun-AK47-SingleShot

Assign the audio file to the AudioSource component and disable the Play on Awake.

Lesson 5: Particle Systems and Audio

Since the AudioSource component is applied to the weapon GameObject we don’t need a public reference because we can access it directly. Place the following code also inside the input if statement.

FULL CODE

WeaponParticlesController.cs

 Weapon.cs

Names to keep

What’s next…

On the next lesson we will start coding our targets movement.

 

 

 

Series Navigation<< Lesson 4: Shooting with Unity 3d raycastingLesson 6: Creating the Targets >>

Leave a Comment

Your email address will not be published. Required fields are marked *