Continuing on fromof this tutorial, this article will guide you through setting up some Particle Effects for the weapon, including muzzle flash, smoke, and shell ejection.
Muzzle Flash Effect
If you take a look at what we've got now, you may notice the weapon isn't displaying any particle effects. This is because the basic script we did in Part 2 of this series didn't define any particle effects to use!
Let's start by adding the most obvious one of all, the "muzzle flash". This is the fire ball and bright light which comes from the end of the weapon's barrel each time it fires a bullet.
To get this particle effect to start playing we need to add a small bit of code (copied from the Rifle.xml) to the weapon script and save the file:
This should go in the "default" <firemode> section, just after the "<fire> ... </fire>" block.
The reason for putting it there and not in the "Rapid" firemode is here it will work in all firemodes, and we don't need the muzzle flash effect to differ depending on firemode.
If you've got Sandbox already opened then reload the scripts via Menu -> Tools -> Reload Scripts -> Reload Item Scripts or type i_reload into console.
Jump in game (CTRL+G) and test it out:
So we notice one big problem, the muzzle flash effect is in the wrong location! This is because the helper we defined in the script doesn't exist on this weapon yet. So let's create it!
It's worth noting that the muzzle flash effect is currently spawning from the asset's pivot point. Keep in mind that any kind of positional information that is setup incorrectly will result in the pivot point being the default fall-back position.
Third Person Weapon Helpers
For first person weapons, helper positions are setup via a file we are yet-to-create, so for now we'll create the third person helpers which are done in Max and are embedded into the model.
On the Create tab, click on the 4th button, Helpers and then under the "Object Type" select the Dummy option.
Now in the scene, drag the mouse to create a rough cube shape, position or size doesn't matter but you may find it beneficial to keep it moderate in size.
When the Dummy is created, position it so that it's at the end of the barrel, where you would like the muzzle flash to spawn.
Rename the Dummy node to weapon_term, and make it a child of the newgun_tp node. Now the script should start to make sense!
Save and re-export the weapon and jump in-game to test out the changes.
It's that simple! Go ahead and create additional helpers that you may need. Another one called "shells" will be created for the shell reject effect below.
Shell Reject Effect
Another cool effect to add is the shell rejection effect which is a particle that displays bullet casings flying out of the weapon on each shot.
Add the following code block just below where the muzzleflash codeblock was added in the default <firemode> section.
Save the script, reload items, jump in game and check it out again:
A small box was also created on the weapon as a visual representation of the shell ejection port.
First Person Effects
Now that our weapon is looking pretty good in third person, let's see how it looks in first person:
Instantly we notice something is broken here. The muzzleflash effect is coming from the position where I picked this weapon up.
As mentioned earlier, the first person weapon gets its helper information from a different file, the .cdf or Character Definition File.
We can also see that the first person view isn't too great, we can't actually see the rear sight when ironsighting.
Read on to the next article (coming soon) where we show how to setup the first person weapon!