Muzzle Flash in Unity

In the previous lesson, we got the soldier to fire at us and cause us damage. In this lesson, we'll add a muzzle flash to the Soldier's gun, so that we can see he's firing at us. The idea for a muzzle flash is to add a Plane to the end of the gun, add a muzzle flash texture to the Plane, and then switch it off and on when he fires. However, there is a problem.

The problem is to do with the soldier model. If you expand his entry in the hierarchy, then expand the Bip001 item, you'll notice two more items, Footsteps and Pelvis. Expand the Pelvis item. Now expand the following:

Bip001 > Bip001 Spine > Bip001 Spine > Bip001 R Clavicle > Bip001 R UpperArm > Bip001 R Forearm > Bip001 R Hand > Bip001 R Finger0.

Finally, you'll come to an item called Bip001 R Finger0Nub. It looks like this, in the Hierarchy:

Game object attached to a model's finger

The thing to notice here is that final item is not the end of the gun. Which means that we'll have to put a muzzle flash on the end of his finger and do our best to position it correctly. This is extremely fiddly, though. But let's see how to get a muzzle flash, all the same.

So, right-click on Bip001 R Finger0Nub. From the right-click menu, select 3D Object > Plane. Rename it to MuzzleFlash. It's going to look huge, so, in the Inspector, change the X, Y and Z Scale values to 0.03. Change the Rotation values to these:

Rotation

X: 40.5
Y: 0
Z: 130

Positioning the muzzle flash is quite tricky, because of the gun issue. So you're going have to experiment. But these values work OK (sort of) for us:

Position

X: -0.3
Y: -0.4
Z: -0.2

The Inspector should look like this: (Ignore the Material at the bottom. You'll add this later.)

The Unity Inspector showing values for a Plane

The Hierarchy should look like this:

The Hierarchy showing an empty game object used as a muzzle flash

And the Scene view should look like this:

Scene view showing a Plane near the soldier's gun

It looks a little bit odd, where it is. But when the soldier starts shooting, the Plane should be in the right place.

Now we need to add a material to the Plane.

First, right-click and save this muzzle flash image to your own computer:

Muzzle flash texture for download

When you've saved it to your computer, drag and drop it into your Textures folder in Unity:

In your Materials folder in the Project area of Unity, crate a new Material. Call it MuzzleFlash. With your MuzzleFlash material selected, have a look at the Inspector. Click the tiny circle indicated by the red arrow in the image below:

Adding a muzzle flash texture to a material

When you get the Select Texture box up, select your Muzzle Flash texture from the list.

Now, at the top of the Inspector, change the Shader. From the from down list, choose Legacy Shaders Standard > Particles > Additive:

The legacy shader in Unity   

Selecting Additive will get rid of the black background.

Now, with your MuzzleFlash material selected, drag and drop it onto either the MuzzleFlash in the Hierarchy or the Plane in the Scene. In Scene view, your plane will change to this:

The muzzle flash texture in Scene view

With your MuzzleFlash selected, switch it off in the Inspector by unchecking the box at the top:

Deactivating the muzzle flash in the Inspector

Now for the few lines of code to switch the muzzle flash off and on when the Soldier fires his gun.

Go back to your EnemyHealth script. Add this variable to the top of the code with all the others:

public GameObject muzzleFlash;

In the FireGun IEnumerator, locate this line:

player.GetComponent<PlayerHealth>().PlayerTakeDamage(damage);

Now add these three lines below it:

muzzleFlash.SetActive(true);
yield return new WaitForSeconds(0.3f);
muzzleFlash.SetActive(false);

Your FireGun IEnumerator should look like this:

Unity C# code for a muzzle flash

Save your code and go back to Unity. Select the Soldier. Now drag and drop your MuzzleFlash item in the Hierarchy to the empty Muzzle Flash slot in the Inspector:

Dragging the muzzle flash from the Hierarchy to the Inspector

Play your game again and you should see the muzzle flash when the Soldier shoots. The animation and the sound should now be more or less in sync. It should look like this, in the short video below: (13 seconds)

 

You could use the same principal outlined above to add a muzzle flash to the player's gun. But we'll leave it there. In the next lesson, we'll tackle respawn points for when the player dies.

<--Back to the Unity 3D Course Contents Page