Search Unity

Unity muzzle flash prefab.

Discussion in 'Getting Started' started by Harry_Hegarty, Apr 26, 2020.

  1. Harry_Hegarty

    Harry_Hegarty

    Joined:
    Mar 30, 2020
    Posts:
    2
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Likely you would approach this as follows. If you are using some prebuilt FPS system, it may have some or all of this implemented for you.

    On a script on your player or gun you have a public GameObject reference to your muzzle flash prefab. You place an empty GameObject at the point on the barrel where you want the muzzle flash to appear, and orient it in the direction you want the muzzle flash to point. When you fire the gun, in that same code which runs you instantiate an instance of the muzzle flash prefab, set it as a child of the empty GameObject on the barrel, and set its rotation and position to all 0's (so it matches the position/rotation of the parent). You then destroy the instance of the muzzle flash after it finishes its animation (destroy the instantiated instance, not the original prefab of course).

    Documentation on instantiate:
    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    It should be easy to find videos or tutorials on how to instantiate prefabs as well.