In this post I will create shooting functionality for weapons which will be easy to work with later.
- I want to have couple of ammo types, (plasma, conventional, dum etc) which can be upgraded later,
- Aiming should be physically accurate,
- There should be impact effects,
<strong>This Tutorial has been created using Unreal Engine 4.8.2</strong>.
Make sure you are working on the same version of the engine.
Creating Ammo Information
First let’s create new ENUM AmmoType and add:
These are all that came to my mind. Definitely I will update ammo types later. If you have ideas for ammo types feel free to write in comments.
Then we need a structure with all data for our ammo.
Create new Structure and name it AmmoData.
- AmmoType (ENUM AmmoType),
- Damage, (float)
- CritChance (float)
- DamageOverTime, (float)
- DamageOverTime_TimeLeft (float)
Now go to BP_MainWeapon and add those variables:
- SpreadMin (float),
- SpreadMax (float),
- SpreadCurrent (float),
- AmmoData (AmmoData Structure)
- TrailFX (Particle System),
- ImpactEffect (ImpactEffect class reference – we will create this class in a sec!)
We need a function to get information where we should spawn projectile or do a line trace (instant fire). Basically getting shoot traces isn’t so simple and you should read this blog post by Mougli (from UDK) and watch this tutorial from RATSGAME before moving forward.
Create a function named CalculateShootInformations.
- Camera (Camera Component),
- WeaponMesh (Scene Component)
We don’t know if our next weapons will be skel meshes so you can use Scene Component here,
- WeaponFireSocketName (name)
- ProjectileTransform (Transform),
- HitResult (Hit Result structure),
- EndLocation (Vector)
- LocalTransform (Transform),
- LocalEndLoc (Vector),
- LocalCamera (Camera Component),
- LocalWeaponMesh (Scene Component),
- LocalWeaponSocketName (name)
As you can see the first thing here I’m assigning inputs to local variables. It’s good practice if your function will use inputs a lot.
That’s all in BP_MainWeapon.
Conventional ammo type.
Go to ShooterGameInstance and create new variable named Ammo_Conventional from AmmoData Struct. You can leave variables as 0 for now because we won’t be dealing any damage.
This will be information about conventional ammo. It can be upgraded later and Pistol will use this data for his AmmoData.
Creating Impact Effects
Create new blueprint based on Actor and name it ImpactEffect.
Lot’s of variables to add here:
- Defaul FX (Particle System),
- Default Sound (Sound Cue),
- Default Decal Mat (Material)
- Decal Size (float, default 2),
- Decal Life Span (float, default 3),
- Metal FX (Particle System),
- Flesh FX (Particle System),
- Glass FX (Particle System),
- Concrete FX (Particle System)
- Metal Sound (Sound Cue),
- Flesh Sound (Sound Cue),
- Glass Sound (Sound Cue),
- Concrete Sound (Sound Cue),
- HitResult (Hit Result, exposed on spawn!),
- RandomDecalRotation (Rotator),
You should copy Effects and Sounds folders from Shooter Example to your project. If you don’t know how look here.
Assign some defaults to FX and Sounds. Now you need to go to project settings -> physics and add some Surfaces.
In Begin Play of this actor we will spawn sounds and fx on impact.
Now create new blueprint based on ImpactEffect and name it ImpactEffect_Pistol. If you have more weapons you can now easily change impact effect for different weapons! Remember to put down ImpactEffect class variable to BP_MainWeapon.
Fire and spawn impact effects!
Go to your BP_PistolWeapon (i will use it as example) For ImpactEffect choose ImpactEffect_Pistol.
Now we just need to calculate the fire (function from BP_MainWeapon) and spawn all effects.
For TrailFX I’ve used P_AssaultRifle_Trail from Shooter Example.
To use AmmoData create Begin Play event and set AmmoData from ShooterGameInstance.
And that’s all! If you want to have debug crosshair go to GamePlayHud and add this:
Basically Shooter Example is great place to learn keep your time and investigate each functionality there!
Implementing game is taking time but writing about this is taking much more effort!