Weapons of some sort are a common feature of many games and muzzle flashes greatly enhance the appearance and feeling when you fire. This recipe will show a way to create good-looking muzzle flashes by tweaking a ParticleEmitter's properties. The following screenshot shows a texture with four muzzle flashes:
There are two things needed before we can begin work on the ParticleEmitter:
ParticleEmitter
.Material
using the texture by performing the following steps:Now, we can begin creating the muzzle flash emitter:
[Sphere, 0.0, 0.0, 0.0, 0.05]
.1
, and Particles Per Sec should be 0.0
.[1.0, 1.0, 0.4, 1.0]
and End Color to [1.0, 0.6, 0.2, 0.7]
.1.0
.0.15
.[0.0, 0.0, 0.0]
.[0.0, 0.0, 1.0]
.All these values can be seen in the following screenshot:
The muzzle flash works pretty much like a normal ParticleEmitter with a couple of exceptions. Instead of outputting a constant stream of particles, it will only emit one. This is because Num Particles is set to 1
, meaning only one particle can be alive at any given time. Particles Per Sec is 0.0
so it won't continuously emit anything.
The colors are set to be yellowish, turning a bit orange and fading slightly at the end of the lifetime; the lifetime being very short in this case, only 0.15 seconds.
A muzzle flash is emitted in one direction only. This is why we set Facing Velocity to true
so that the particle will point in the direction of the velocity.
Getting it to appear in the correct position in relation to the weapon can require a bit of tweaking. Using Local Translation can help us in this.
To use the muzzle flash on a weapon, open the target in Scene Composer and then choose Link in Scene on the muzzle flash. This way the original file can be modified and the changes will automatically appear in the places its being used.
Now that we have the muzzle flash and added it to a weapon, we can create a control in order to use it within the game by performing the following steps:
WeaponControl
extending AbstractControl
.ParticleEmitter
field called muzzleFlash
.setSpatial
method, check whether the supplied spatial has a suitable child, either by type or name (requires that the muzzle flash has a fixed name), and set the muzzleFlash
field:muzzleFlash = (ParticleEmitter) ((Node)spatial).getChild("MuzzleFlash");
onFire
method and add the following:if(muzzleFlash != null){ muzzleFlash.emitAllParticles(); }
This control should then be added to the weapon spatial inside the game and onFire
should be called whenever the weapon fires. The class is suitable to play sounds and keeps track of ammunition as well.
18.191.139.169