Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make muzzle flash disappear when not firing?

Discussion in 'Scripting' started by Treasureman, Mar 17, 2015.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a sentry in my game that locks onto a player and starts firing when the player is seen by it. Here's the sentry... Screenshot (110).png
    ...and when it sees you a gameobject appears that's supposed to be a muzzle flash. Here's what that looks like... Screenshot (111).png
    But my problem is whenever the sentry loses you and stops firing, the flash stays there... Screenshot (112).png
    How would I make it disappear whenever it's not firing at you? Here's the script that makes it appear (I think). I got it at the asset store so I don't know much of how to change it.
    Code (JavaScript):
    1. var muzzleFlashPrefab : GameObject;
    2. var materials : Material[];
    3. var rate : float = 8.0;
    4. var on : boolean = true;
    5.  
    6. private var nextMuzzleFlashTime : float;
    7.  
    8. function Update () {
    9.     if (on){
    10.         if(Time.time > nextMuzzleFlashTime){
    11.             nextMuzzleFlashTime = Time.time + (1.0 / rate);
    12.             var newMuzzleFlash : GameObject = Instantiate(muzzleFlashPrefab,transform.position,transform.rotation);
    13.             var materialId : int = Mathf.RoundToInt(Random.Range(0,materials.Length));
    14.             newMuzzleFlash.renderer.material = materials[materialId];
    15.             newMuzzleFlash.transform.parent = transform;
    16.         }
    17.     }
    18. }
    Here's the asset on the store if you want to test it yourself...
    https://www.assetstore.unity3d.com/en/#!/content/1727
     
  2. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    Just disable the muzzleflash object's mesh renderer (and possibly light, if it has one) after a short amount of time (~0.1 seconds) after firing. When firing, you enable it.

    Edit: From looking at the script, it seems like the muzzleflash gets instantiated every time the gun fires so in that case, you jsut have to store the muzzleflash obejct in a variable and destroy it after a fraction of a second. However, this is generally not advised, so it would be far better to have the muzzleflash object always there or instantiate it once when the game starts, but keep it's renderer disabled, and only enable it shortly when firing, as described above.
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I removed the mesh renderer from my empty game object (the mesh is in an empty gameobject so It looks right when coming out of the gun barrel), and I got an error saying that it didn't have the renderer (it didn't specify mesh renderer) and the script was looking for one. When I deleted it from the actual mesh, it just didn't appear.
     
  4. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    Hold on a second, your post doesn't make sense to me. You say you removed the mesh renderer - why? If you have no mesh renderer, how is the muzzle flash supposed to be shown? I wrote to disable it via .enabled = false, not remove it from the object altogether. Then you say "the mesh is an empty gameobject". How can a mesh be an empty gameobject? An empty gameobject has no component other than Transform. Please clarify so I can help you :)