Weapons – shooting and reloading functionalities

In this post I will focus on shooting and reloading functionalities. Goals that I want to achieve:

  • I want to have easy method for shooting with different weapons such as the famous survival shotgun.
  • If my weapon doesn’t have ammo it should reload automatically,
  • I can reload my weapon whenever I want,
  • I won’t be able to shoot if I’m changing weapon,
  • Show some modifications with the upper parts for AR-15’s

In this post weapon won’t fire any projectile yet but you will learn more about blueprint communication, creating animations in blueprints and doing recoil directly in animation blueprint, also if you’re interested in firearms and shooting you can get experience in sites like Allaboutshooting online that will help you understand this better so you can apply it in your work.

Adding base variables to BaseWeapon. Basically BaseWeapon should have variables / functionalities that all weapons can share. Open BP_BaseWeapon and add those variables:

  • CurrentAmmoInMag (int) It will tell us how much ammo we have in magazine,
  • MaxAmmoInMag (int, default 6) How much ammo we can load to magazine,
  • CurrentAmmoInBackpack (int) How much ammo do we have currently in backpack,
  • MaxAmmoInBackpack (int, default 30) How much ammo we can have in backpack,
  • RealoadTime (float, default 2) It will tell us how much time is needed to reload the weapon,
  • WeaponType (ENUM WeaponType) It will tell us which type this weapon is. In this post we will use this information for recoil but it should be useful later as well,

Now let’s create functions that all weapons can use:

  • Fire Basically all weapons will have ammo so let’s decrease ammo here, firebaseweapon
  • Reload All weapons need to have reload functionality. We will check how many ammo do we have in CurrentAmmoInMag and set CurrentAmmoInMag according to the MIN value. MIN will return MaxAmmoInMag if CurrentAmmoInBackpack >= MaxAmmoInMag. If CurrentAmmoInBackpack will be 3 or 4 or 1 it will Set those values to CurrentAmmoInMag. So we won’t be reloading full mag if we don’t have enough ammo in backpack. reloadbaseweapon
  • HaveAmmoInMag – outputs HaveAmmo (bool) and MagIsFull (bool) it can be pure function, haveammoinmag
  • HaveAmmoInBackpack – outputs HaveAmmo (bool) haveammoinbackpack

Last thing is to fill CurrentAmmoInMag and CurrentAmmoInBackpack so we start with filled ammo. To do this let’s use Begin Play event. beginplay

That’s all in BaseWeapon.


BP_Weapon_Pistol We will be working on Pistol weapon in this post. You can overwrite MaxAmmoInMag and MaxAmmoInBackpack for this weapon if you like. I will leave it as it is. Now we need to call parent class Fire and Reload functions. To do so:

  • Right click on your event graph,
  • Find Event Fire and add it,
  • Compile your blueprint,
  • Left click on Event Fire,
  • Chose “Add Call To Parent Function”
  • Connect Event Fire with Parent: Fire

howtodosupercall

Basically Parent: Fire will execute everything that is in BP_BaseWeapon. We won’t be using additional functionality for Fire in Pistol but I wanted to show you how to call parent functions. Thou we can play reload animation in Reload function. Please find Event Reload and add it to the Pistol event graph like here: pistolfireanim PlayAnimation will play already existing animation from Military Weapon Silver package. It contains muzzle flash FX and sounds as well! They are pinned to animation using AnimNotifies. That’s all here. Now the biggest part in GameplayCharacter!


GameplayCharacter It will be responsible for managing reloading and animations. Basically we don’t have any hands reload animations that will fit to our weapons so we are doing here lot’s of “hard coding” blueprint animations connected to Animation Blueprint. But it’s working and will work for all of the weapons. Open GameplayCharacter and add this variable:

  • CanFire (bool),

One Dispatcher:

  • OnCharacterFireWeapon and it will take input WeaponType named WeaponType,

Now Custom Events:

  • Reload. I’ve added comments to help you guys to understand this, Event_Reload
  • Fire,
    Event_Fire
  • Left Mouse Button and R button inputs,
    fireandreloadinput

That’s almost all but at this point equipping weapon will be broken because we will be able to start reloading while equipping different weapon and we will see animation glitches when equipping different weapon during reloading. To fix that we need to update EquipWeapon event. I’ve selected added nodes. equipweaponupdated

In earlier post we EquipWeapon event had branch for isReloading but nothing goes to TRUE. Added blocks are connected to True. Everything else stays the same. Basically what this is doing its checking if hands are still up. If yes – move them down but do this with FInterpt because we don’t know if the value will be 0.5 or 0.1 or 0.9 or 1. If hands are down we can change weapon normaly. Thanks to that you will have seamless changing weapon animation during reload! PullWeaponDown timeline is used as a Tick. It is 5 sec length to make sure that FInterpt will be able to reach Target 1.

USEFUL TIP: You can use timelines as spawning Ticks for your game. They will Tick only when they are active.

Now functionality is here but we don’t have any recoil animation. Let’s do that.


Creating custom recoil. Basically recoil should be done using hands animations but we don’t have any. That’s why we will create custom recoil using Animation Blueprint! Open HeroFPP_AnimationBlueprint and add these variables:

  • PistolRecol (bool),
  • DeltaT (float),
  • CurrentRecoilTime (float),

Now in Event Graph let’s update Update Animation event:
recoileventgraph
What this is doing its waiting for PistolRecoil will be true. If yes it’s adding DeltaTime to CurrentRecoilTime and if it’s taking 0.2 seconds we are disabling recoil (PistolRecoil) and resetting CurrentRecoilTime to 0 so we can use it again. Now create new event Blueprint Initialize Animation – it’ something like Begin Play in other actors. We will use this to connect to CharacterFireWeapon dispatcher we created in GameplayCharacter. bindfiredispacher
Every time this Dispatcher will fire (so player will fire) we will set PistolRecoil to true. Now we need to drive PistolRecoil bool in AnimationGraph.
recoilanimgraph
In earlier post you have learnt how to use Transform (Modify) Bone nodes. You can use them for recoil here. I won’t be posting screenshots of my Transform Bones nodes details. Try to create them by yourself. You will learn a lot! The most important part here is Blend Posses by bool. If PistolRecoil is true then we are doing modifications to animation if not – we are in PistolPose without any modifications.

USEFUL TIP:  In Animation Blueprint you can change PistolRecoil bool in AnimPreviewEditor to test out how your recoil will look like. testpreview

That’s it! You can test it out using left mouse button and R keyboard input.


Creating ShooterTutorial takes a lot of my free time.
Buy Now Button
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!

15 thoughts on “Weapons – shooting and reloading functionalities

  1. Pingback: FX: Create ammo shell particle from mesh | Shooter Game Tutorial

  2. Never mind, got it working. Although it doesn’t work as soon as I spawn the player. Need to equip the weapon obviously and mess a bit with reloading and swapping weapons with 1, 2, 3 before pistol start to shoot.

  3. very good work !
    please dont stop ! your tuto help me to build a game !
    can you add a download link after evry tuto in order to compare ..

  4. In this post you say that the event EquipWeapon had from an earlier post a branch isReloading. I have checked previous posts and do not find that. Could you post or send me the full image of your EquipWeapon Event. Or at least an image showing the beginning up to the branch you have in the image above.

  5. Thank you for these tutorials by the way, I just found them from a Unreal Engine twitter post the other day. It helps with some different aspects of the engine that I’ve been trying to figure out on my own. I appreciate the amount of time and effort you put in these, so thanks again.

  6. Welp made is this far before I got stuck. FUCK. My issue is that in the BP_Weapon_Pistol, for some reason my Play Animation node says “Target is Skeletal Mesh Component” instead of “Target is BP_BaseWeapon_C” like it says above. So, I figure this is why the animation isn’t firing.

    I have absolutely no idea how to fix this problem so I guess I’m done with this tutorial for now. I’ll return if someone provides me a solution. And yes, I am using version 4.8.

    • Hi, I’m going through these tutorials several months after you did (thank you so much, unnamed author! I’ll be donating next time I get paid). I happened on this same problem, and it’s a tricky one if you are new to Unreal like I am.

      If you’re making the weapon up / down timeline nodes in the character blueprint, following the screenshot, it’s likely when you want to add the call to Reload you’ll right-click and start typing Reload. What that will give you is a call to the custom event you just now created, the one at the left of the screenshot! What you actually want is a call to the function on the weapon, which also happens to be named Reload. To get that: drag a get reference to your current weapon variable from the sidebar into the graph. Drag a connector out from that, let go of the mouse to show a search box, and in there search for Reload. That’s the node you want to add here.

      It would help if these were named something like “CharReload” and “WeapReload” so you’d see the problem right away… but perhaps this is best left alone, as an object lesson in one of the fundamental truths of programming: naming things is hard.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.