gun muzzle flashes

ive been making this fps game with unity for quite a while now i have made my own guns and characters and stuff like that but i dont know how to make a muzzle flash or an explosion from my gun when i shoot.

can anyone help me?

What a lot of people do is add a plane just at the end of the muzzle, and apply an image of muzzle flash. Then add a code on your gun somewhere that randomly rotates the plane slightly when your fire to give the effects of ... randomness I guess.

Take a look at the FPS tutorial by Unity - they do the same thing (

Something like this:

    var muzzleFlash : Renderer;
private var nextFireTime = 0.0; 
private var m_LastFrameShot = -1;

// We shot this frame, enable the muzzle flash 

if (m_LastFrameShot == Time.frameCount) {
   muzzleFlash.transform.localRotation = 

   Quaternion.AngleAxis(Random.Range(0, 359), Vector3.forward);
   muzzleFlash.enabled = true;

// We didn't, disable the muzzle flash 
else {
   muzzleFlash.enabled = false; enabled = false;

The reason everyone is coming up with an error is because he didn't have a } at this point in the code:


<p>} //<-just added</p>

<p>// We didn't, disable the muzzle flash </p>

<p>else {</p>

<p>muzzleFlash.enabled = false; enabled = false;


(Sorry don't know how to do the code box thing yet)