MuzzleFlash for FPS game

Hello,

This is really embarrassing, but I do not know how to create a muzzle flash for my game. I have set up a plane with a muzzle flash texture on it, but I don't now how to code it.

All help is appreciated.

-Tate

What I'd do (in JavaScript) is attach the muzzle flash object as a child of your weapon (so it moves when your weapon does), and make sure it is set as off by default (The little checkbox to the left of it's name in the inspector). Then use a variable in the weapon's script (the same script that shoots) to identify it; like `var muzzleFlash : GameObject;` and also make another variable (probably make it private after tweaking it to look right) `var muzzleFlashLength = 0.1;` (that's just a number I pulled outta nowhere, might look bad).

When you call your fire function (or wherever you make your gun fire), call another function, maybe `MuzzleFlashOn();` so that whenever your gun fires, this function is called. I'd script it as

function MuzzleFlashOn()
{
    muzzleFlash.active = true;
    yield WaitForSeconds(muzzleFlashLength);
    muzzleFlash.active = false;
}

the GameObject.active control activates and deactivates the referenced Game Object accordingly (that same checkbox mentioned earlier). If you want to stop there, that should work fine, but if you want to spruce up the muzzle flash, you can add a line changing the rotation to a random one, and even stretch it a bit (Probably best to make a variable for that too, so you can tweak it to look good) every time you call the function.

Here's the whole chunk of script I mentioned:

var muzzleFlash : GameObject;
var muzzleFlashLength = 0.1;
var maxScale = 0.25;

function Update () 
{
    if (Input.GetButtonDown ("Fire1"))
    {
        //The rest of your firing stuff
        MuzzleFlashOn();
    }
}

function MuzzleFlashOn()
{
    muzzleFlash.transform.eulerAngles.x = Random.Range(0.0, 360.0); // I prefer eulerAngles to Quaternions
    muzzleFlash.transform.localScale.x = 1 + Random.Range (-maxScale, maxScale); // the 1 + bit is to reset the scale every time
    muzzleFlash.active = true;
    yield WaitForSeconds(muzzleFlashLength);
    muzzleFlash.active = false;
}

Hope that helped!