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,
Let’s start!
This Tutorial has been created using Unreal Engine 4.8.2. Make sure you are working on the same version of the engine.
Creating Ammo Information
First let’s create new ENUM AmmoType and add:
- Conventional,
- Plasma,
- Laser,
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.
Inputs:
- 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)
Outputs:
- ProjectileTransform (Transform),
- HitResult (Hit Result structure),
- EndLocation (Vector)
Local Variables:
- 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!
Creating ShooterTutorial takes a lot of my free time.
If you want you can help me out! I will use your donation to buy better assets packs and you will be added to Credits /Backers page as well.Implementing game is taking time but writing about this is taking much more effort!








Pingback: How to add spread and dynamic crosshair to weapon | Shooter Game Tutorial
Just solved a Problem from a User on FB that followed your Tutorial. The SpawnActor Node has no HitResult Input. You forgot to mention that the HitResult Variable need to be set to “Expose on Spawn” to show Up in the SpawnActor Node.
=)
LikeLike
Pingback: Create an Grenade Launcher | Shooter Game Tutorial
I got a problem with shooting direction. my editor can’t access local camera and weapon shoots wrong direction
LikeLike
Can I send you my project? So you can check it why the code cannot identify end location for the camera. I have already checked it several times. I even checked your blueprint and cannot find the difference. I thought that the problem is in editor itself( i had UE4.7.6) but I updateed it up to UE4.8.2, the problem still there.
Can I send you my project, so you can check it please?
LikeLike
To fix the above (I had the exact same issue), in Fire (BP_BaseWeapon), change get owner to get player character. This will allow it to successfully get the camera details (instead of accessed none) and thus complete the spawning of the decals, sounds and effects.
LikeLike