Muzzle flash in Unity

Hello everyone, welcome back.

Today’s post is about muzzle flash and for this, I am using Unity version 2023.1 and I have a project setup in URP. I also have the post processing volume with main modules Bloom and Tone mapping with the mode Aces.



Theory

Let’s discuss some theory about muzzle flashes, so basically they are the burning gases that shoots out from the barrel along with the bullet itself.

Here is the actual reference of the muzzle flash. Courtesy: Polenar Tactical.




And to recreate this, we will use VFX graph, I have already gone ahead and installed the VFX graph from package manager.

And I have this AK model from the OG God of unity tutorials, Asbjorn, the CEO of team brackeys.

Okay so our AK has an empty child gameobject, firepoint from where our bullets will spawn. We will add Visual effect component on it.

Now let’s create a VFX graph so,

Right click > create > Visual effects > Visual effect graph.

Let’s call it, MuzzleFlashVFX, and assign it to our firepoint’s visual effect component.

Output context

In the VFX graph, let’s give a proper name to our system then the first thing we want to do is, in Output context, we will set the Main texture of our particles. I am going to use the default simple smoke texture then set the Blend mode to Additive.

Then in Set size over life attribute, I am going to adjust the curve so that our particle starts very small and quicky grow very big.

In Set Color over life we will set some color. We want to control the color from the inspector so let’s create a Gradient property. Let’s call if MuzzleFlashGradient. Now we want to make it look like burning gas and overtime make it look like smoke. So at the 1st notch, I will set some yellowish color with high intensity, then will add 2nd notch at around 30 % give some grayish color with 0 intensity, and the 3rd notch will be full black. For the alpha, I will bring this notch at around 20 % and set the alpha around 80.

Then drag it in and feed it into the color, also make sure that we have set the Color mode to Color and Alpha otherwise the alpha values will be discarded.



Spawn context

In this Spawn context, instead of continuously spawning particles we want bursts, so let’s delete Constant spawn rate attribute and add Single burstWe want random numbers of particles in each burst so set the Spawn mode to Random. Set the Count between 10 to 50.



Initialize context

Now in the Initialize context, let’s set the Capacity to 300, so any particles above 300 will be discarded and I will set the bounds in front of our AK barrel.

The bounding box is the culling area for our particles, any particles outside this box will not be rendered, also if this bounding box is not visible to our camera, no particles will be rendered and we don’t want any padding.

Now we have this Set velocity random attribute, and this Per component means different random numbers for each component, so different values for X, Y and Z. We will set Z between 3 and 6, but for X and Y, I want random range for every burst so let’s create Vector2 property. Call it SpreadRangeX, I will give default value of 1, 2 and drag it in.

Let’s create a Random number operator. It will give us random number between min and max. Uncheck this Constant, so we will get new random number each time. Then Feed our SpreadRangeX property’s X in Min and Y to Max. Then take its output and feed it into the X of Random velocity A.

Take another output from our Random number operator and feed it into Negate operator. Negate operator will multiply any value we feed in with -1. Then take Negate operator’s output and feed it into the X of Random velocity B.

We will do the same for Y so let’s create another Vector2 property, I will call it SpreadRangeY and give default value of 0.5 and 0.8 and do the same as we did with SpreadRangeX.

Muzzle flashes are very quick so we will set very small lifetime, so in Set Lifetime Random attribute, let’s set lifetime between 0.1 and 0.3

Now our particles are looking very uniform so let’s set random rotation for them. For that let’s add Set Angle random attribute with Uniform mode. Now we are outputting particles as quads so we only want rotation on Z axis so from the Channel dropdown, select Z and set rotation between 0 to 360.



Update context

Now In the Update context, I want to add some velocity over lifetime, so let’s add Add velocity over life attribute. Now notice in the inspector the Composition mode is Add so this will add velocity on top of the one we have set in Initialize contextNow I don’t want to add velocity on X and Z so in the X curve I will select this linear curve, go to last key, edit it and put 0 in value slot.

We will do the same for Velocity_z, so copy from Velocity_x and paste on Velocity_z.

We will gradually increase Velocity_y so I will set the curve accordingly with max value 0.4.



That’s it for our VFX graph. It will look something like this at the end.




Now notice that, as soon as we hit play our effect automatically gets played, that’s because of this Initial Event Name property. Here we are telling Unity to invoke OnPlay event as soon as we hit play, and OnPlay event is used to play our effect. Simply remove it from Initial Event Name property from the inspector.

Trouble following along? Check out this video!



Thank you so much for reading!

Comments