How to have a light when shooting?

Hi, i need help with this - I have a gun, it shoots, has muzzle flash - but no light when shooting. Do you have any idea how to do it? Maybe like when Fire1 show light at end of the gun for 0.03 seconds. Thank you for any help :slight_smile:

It sounds like you’ll want to create a point light or spot light named MuzzleFlash, make it a child of your gun and position it correctly. Fiddle with the light settings to get the visual effect you want. Then in the inspector, uncheck the checkbox next to its name to turn it off.

Wherever your code is to fire the gun, create a public member for your muzzle flash game object and drag the light into that so that code has access to the light.

Then you can turn on/off the muzzle flash with the SetActive(bool) function.

// Declare your public property.
public GameObject muzzleFlash;


// Code to turn it on
muzzleFlash.SetActive(true);

// Code to turn it off
muzzleFlash.SetActive(false);

To time the duration of 0.03, you can keep up with the elapsed time and track the state of your gun. Or you could use the Invoke method to call a method which turns off the muzzle flash in 0.03 seconds. Or if you want to adjust the settings of the light during that time, you could use a Coroutine.

  • Eck