How to add weapons – basics + equipping

Now if we have our inventory we can start adding new weapons!

  • Player can choose which weapon he want to play, (for now it will be placeholder UMG we did in inventory tutorial)
  • Player can change weapon during gameplay with animation, (by keyboard now)
  • Weapons will attach correctly,
  • Hands will update idle animation to correctly hold the weapon,

We won’t be doing shooting / reloading in this post. Let’s focus on basics first.

For this post I will use assets from Unreal Marketplace. I’ve chosen Military Weapons Silver.


New Enum: WeaponType

Create new Enum named WeaponType and add: Pistol, Rifle and Shotgun to it. You should know how to add Enums from reading Adding Inputs Tutorial.

BP_BaseWeapon

First we need to create base class for all of the weapons. Each weapon will extend from this class. Thanks to that we will be able to do some functions on them like Fire, Equip or getting variables like Ammo without knowing the exact weapon. This will be something like object-oriented programming.

Create new Blueprint extending from Actor and name it BP_BaseWeapon. Now inside:

  • Create scene component as a root. This is something that I’m always doing when creating new blueprint,
  • Create Skeletal Mesh Component and name it WeaponMesh,
  • New variable: IndexInBackpack (int) which should be Editable and Exposed On Spawn,
    We will be passing this from Backpack_Weapons in ShooterGameInstance,
  • New variable: AttachSocketName_FPP (name) default value: WeaponPoint
  • New variable: AttachSocketName_TPP (name) default value: WeaponPoint
    Those will be used to attach the weapon to FPP or TPP meshes.
  • New variable: WeaponType (enum WeaponType)
    This will hold information for animation blueprint. Hands need to update animations for different weapon types.

For now it’s it. Later this blueprint will have variables like CurrentAmmo, MaxAmmo etc.


Updating WeaponBackpackItem structure.

Before we go further we need to update WeaponBackpackItem structure we had created earlier. WeaponToSpawn shouldn’t be Actor. It should be Class reference to BP_BaseWeapon. We will be using SpawnActorFromClass node later and we need reference to the class not to the actor.
weapontospawnclass

USEFUL TIP: If you change structure variable name or type everything connected to it in blueprints will be disconnected! You need to double-check if everything is connected after changing something in Structures.


BP_Weapon_Pistol

Create new blueprint based on BP_BaseWeapon. Name it BP_Weapon_Pistol.

  • In Components tab select WeaponMesh and chose Pistols_A skeletal mesh from Military Weapons Silver,
  • Now you won’t see inherited variables from BP_BaseWeapon. To see them you need to click on small eye 😉
    showinheritedvariables
  • After that you will be able to see variables created in BP_BaseWeapon and you can overwrite them!
  • WeaponType should be Pistol,
  • AttachSocketName_FPP should be S_PistolSocket

That’s all here, let’s create another weapon.

BP_Weapon_AssaultRifle

Again create new blueprint based on BP_BaseWeapon. Name it BP_Weapon_AssaultRifle.

  • WeaponMesh should be Assault_Rifle_A,
  • WeaponType should be Rifle,
  • AttachSocketName_FPP should be S_AssaultRifle,

BP_Weapon_Shotgun

The same thing as above. WeaponType should be Shotgun, AttachSocketName_FPP should be S_Shotgun and WeaponMesh Shotgun_A.

That’s it. We have three weapons now!


Sockets

Open Hero_FPP_Skeleton.

On the left side you are seeing bones from skeleton. You can create sockets connected to bone. It can have relative location and rotation. We will use sockets to attach our weapons. This documentation from Epic is ideal place to learn about creating sockets and previewing meshes. I won’t be posting about how to create sockets – please read documentation.

Create these sockets from b_RightWeapon bone:

  • S_Shotgun:
    – Relative Location: (X=0.299006,Y=1.298091,Z=8.979076)
    – Relative Rotation: (Pitch=85.000000,Yaw=90.000000,Roll=0.000000)
    – Relative Scale: (X=1.200000,Y=1.200000,Z=1.200000)
  • S_AssaultRifle:
    – Relative Location: (X=0.299006,Y=0.528186,Z=9.127792)
    – Relative Rotation: (Pitch=85.000000,Yaw=90.000000,Roll=0.000000)
    – Default Relative Scale 1,1,1
  • S_PistolSocket:
    – Relative Location: (X=0.714007,Y=1.284835,Z=11.106700)
    – Relative Rotation: (Pitch=85.000000,Yaw=90.000000,Roll=0.000000)
    – Relative Scale: (X=1.100000,Y=1.100000,Z=1.100000)

If you are previewing your meshes in sockets don’t be afraid that they won’t fit exactly to hands. We will be using AnimationBlueprint to fix that.


AnimationBlueprint

Animation blueprints controls how skeletal mesh will animate. It can take data from owner (in our example it will be GameplayCharacter) and use it to drive animation. Please read Epic’s documentation before moving forward.

Open HeroFPP_AnimatoinBlueprint and create new variables:

  • BaseRotation – rotator default value: (Pitch=0.000000,Yaw=0.000000,Roll=-6.681010)
  • PullDownPercentage – float, default 0
  • CurrentWeaponType – WeaponType Enum,

We will be assigning those variables in Event Graph.

animbpeventgraph

Now go to Animation Graph. Create something like this:

firstpart

  • Its taking FPP_LauncherAim animation,
  • Then it’s connecting it to slot Normal, slots are mostly used for playing Montage Animations. If montage will be played it will go here to this slot. Please watch this tutorial form Epic. If you have time you should watch the full playlist. Montages documentation can be found here.
  • Then it’s adding rotation to RightHand bone using BaseRotation variable,  (without that our gun will be pointing up)
  • After that it’s modifying b_Spline1 bone using Alpha (from 0 to 1) – it’s used to control equipping animation,
  • Then it’s caching the pose to new cache called BasePose so we can use it later.

Now we need to blend pose by Enum – by our WeaponType.

blendposebyenum

blendposebyenumfinalanimation

You will be able to fill all of the TransformBone blocks but there is a new block called TwoBoneIK.

It should look like this:

twoboneIK

Basically what it’s doing it’s taking b_LeftForeArm and trying to change bones location to focus on b_RightHand. If you select this node in Event Graph you will be able to change the location!

I’m using it to change left hand location to hold the weapon. TwoBoneIK is powerful and you should learn it!

Here you can find the whole animation blueprint.

animblueprint

That’s all in Animation Blueprint!


Preparing GameplayCharacter

Gameplay Character will be responsible for spawning weapons, attaching them to slots and changing them. Open GameplayCharacter and add some variables:

  • WeaponSlot_1 (BP_BaseWeapon)
  • WeaponSlot_2 (as above)
  • WeaponSlot_3 (as above)
    Here we will store references to weapons that we have chosen in Inventory before gameplay,
  • CurrentWeapon (BP_BaseWeapon)
    This will hold current weapon reference so we can check which weapon player is holding,
  • isReloading (bool)
    This will tell us if hands are doing reloading animation,
  • isChangingWeapon (bool)
    We need to know if we are currently changing weapon,
  • WeaponPullDownPercent (float – default 0)
    This will be used for put weapon down and up as a changing weapon animation. We don’t have any change weapon animation so we need to create one,

After variables let’s move to functions.

  • GetShooterGameInstance – function like in other classes we have created, pure function which will return ShooterGameInstance,
  • SpawnWeaponsAndAssignToSlots
    This is important function. It will spawn meshes and plug them to WeaponSlot_X
    spawnweaponsandassigntoslots
  • ShowWeapon – input “BP_BaseWeapon” named “WeaponToShow”
    This is helper function to hide and show weapons in slots.
    showweapon

Those are all functions for now. We need to create one Custom Event in Event Graph because we will use Timeline to drive equip animation.

Create new custom event “Equip Weapon” with one input “BP_BaseWeapon” named “Which Weapon”
equipweapon

We are using Timeline. If you don’t know what’s Timeline please read Epic documentation.

Our timeline is driving float variable and a event.
equipweapontimeline

Basically it will tell AnimationBlueprint to rotate hands down and up. When hands are down we are firing event to show the weapon we are equipping.

The last thing in Event Graph is to create key bindings for 1,2,3 and fire EquipWeapon event.
keybindings

That’s all in GameplayCharacter!

Now if you want to test this out just add a button to UI_Debug_WeaponSelection like this one:

spawnweapons

Take your time to learn animation blueprints and object oriented blueprinting.


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!

93 thoughts on “How to add weapons – basics + equipping

  1. Pingback: Weapons – shooting and reloading functionalities | Shooter Game Tutorial

  2. Pingback: Creating a Shotgun | Shooter Game Tutorial

  3. Pingback: Creating an Assault Rifle | Shooter Game Tutorial

  4. Hi
    Thanks for your great tutorial.
    I have done all parts you explained in this tutorial but when i play the game and choose weapons from inventory (like your gif on top of this page) , my weapons don’t appear .And button 1 , 2 and 3 don’t do anything.(I use UE 4.7.6 )
    what should i do ?Is there anything that i should do ?

    • “The last thing in Event Graph is to create key bindings for 1,2,3 and fire EquipWeapon event.” – this need to be done in GameplayCharacter.

      Make sure you have added this “Now if you want to test this out just add a button to UI_Debug_WeaponSelection like this one:” to your button in UMG.

      It’s hard to say why it isn’t working for you – if you can send me your project on mail: shootertutorial@gmail.com and I will be able to take closer look.

      • Hi
        I sent an email with subject “My Project”.
        I think the problem is i have not used IndexInBackpack variable but i don’t how to use it and it’s not in this tutorial(you created it in this tutorial but you didn’t use it anywhere).
        Thanks for helping

    • Yes i did . I also used your blueprint folder and everything was ok but in this tutorial the only problem is when i choose textures in inventory(like you did in gif)for my weapons it doesn’t do anything and everything is fine like selecting and deselecting.
      Thank you

  5. Im having an issue where in GameplayerCharacter > SpawnWeaponsAdnAssignToSlots when trying to spawn the actor for each weapon slot. In the picture you provided the SpawnActor node has an Index in Backback input integer, but I have no idea where that came from because it is not included in my project, and it wasnt created anywhere else. Here is a picture.

    http://i.imgur.com/fBnHUTy.png

    • It’s an issue with Editor – will post that in Answer Hub. What you need to do is to create node Spawn Actor from Class and select BaseWeapon as a class. Then you should see Index In Backpack int (if you had selected “expose on spawn”) and you just need to connect Weapon To Spawn to Class. It will work. Editor doesn’t see currently which class you are extending if you are moving from wire.

      I will post here screenshots step by step how to do that, but I’m out of my computer now.

  6. First time if you want to assign the BP_Weapon_Pistol, editor doesn’t see it. For it you should change default “Current weapon type” in “HeroFPP_Animation” one by one to make sure that editor see it. I don’t know why but by doing so it worked. I hope it will help to someone with the same problem

  7. Hi,
    If you have the time, could make an v2.0 weapons equipping without inventory ? (just have to press a key to get the weapon) 🙂 ?

  8. i’ve followed all the tutorials but i cant figure out why weapons dont show up, may i send you my project ? can u help me ? thanks for the great work, keep it up man, u are awesome !

  9. Hey everybody! I’m so glad, i finally did it.

    Did the Tutorial yesterday and it did not work. After that i checked every step three times, downloaded the BP Folder and went more or less mad 🙂

    But everything you need to succed is in the tutorial!

    When you are going to do the step: “Updating WeaponBackpackItem structure”, there is a “USEFUL TIP”. Well it’s not really it Tip, it’s a MUST DO!

    In your ShooterGameInstance is the Function “SetBackpackItemseleted”, there is one connection destroyed when you change WeaponBackpackItem to structure. It’s between “BreakWeaponBackpackItem and “Make WeaponBackpackIteam” – the purple guy!

    remake this and everything will work totally fine!
    cu in the next Tutorial:)

  10. Before I ask my question I just wanted to say thank you for this tutorial, it’s been really helpful for getting started in UE4.

    My question is that (like another user) when I start my game I don’t see my guns and my 1-3 keys don’t do anything. I used a different arm rig and weapon models (but I made sure to adjust everything correctly), could this be the issue?

  11. Good tutorial! When i press “I” for the inventory, all works, selecting guns, changing with 1,2,3… but when i press it again and deselect from the slots, the gun mesh dosnt disappear, can you help me? thank you!

  12. First of all thanks for taking the time to make these tutorials, secondly if you are going to use paid assets next time you may want to mention that in the overview first so people don’t waste their time following a tutorial they cant finish.

    • It’s entirely possible import weapons yourself. I got a few models from GameBanana’s Dev Hub and created my own animations. From there you can add the sound/effects in yourself.

  13. I found you get much better results using the rifle idle animation for the base pose and rotating (no translation) the left shoulder bone. Using a Two Bone IK on the left forearm makes the left hand clearly lose sync from the gun when the idle animation is running.

    • That’s the spirit! I really suggest to just take your time and play around with UE4. Create your content don’t copy everything from ShooterTutorial – this is only inspiration! Good luck!

  14. Pingback: Enemy: “Marine” | Shooter Game Tutorial

  15. As other people said, this is straightforward. If you find problems, try to advance to the next section of this tutorial, and come later for the past one.

    If after following the tutorial, nothing happens on selecting the weapons at the ui, check:

    -That all the blueprints are like the images (especially after you change WeaponBackpackItem).

    -That you added the weapons at BackpackWeapons.

    -That you made right GetShooterGameInstance: http://i.imgur.com/78T6ivT.png

  16. Pingback: How to add weapons – basics + equipping | Dinesh Ram Kali.

  17. Hello and thanks for the good tutorial you have made.I have allready done and make me so mad
    with this, all functions worked, but i have a issue with deselecting the weapons from slots.
    They don’t remove from slots and 1,2,3 keys do not everything, but in inventory, i can all weapons select with no problems. Maybe, i have forgotten yet? Can you help me wit this problem via e-mail?

    • Hello Bredock, I have the same issue as you. when I am assigning weapon for the first time it assign correctly, but if I want to change weapon in inventory in slot2 with other weapon, first weapon doesn’t remove from view. So could you help and say how did you solve the problem?

      • Hi Tom ,when you have a equip Launcher, Rifle or Pistol animation montage, you must to assign them in the FPP_Animation_Blueprint in animgraph, if you have not a montage, create one. This is the know what i have.I didn’t really solve the problem how i assign it to the animation blueprint,. but when you not assign the montage you have not equipping the function allready working. I think, you lok at the backpack you have created in the gameinstance one click and on the right side you have details from
        backpack, open all then can see, there are more option to assign the weapons, that’s solves your
        problem.

        Greetz

  18. So i figured out with the 1,2,3 keys, it worked, but there is aother problem with the equipping weapon.
    When i equipt, i lose the launcher_aim animation what do i wrong?

  19. The last Button you have created, where do you adjust it in the UI_Weapon_Debug_Selection inUMG?
    where do i put in the Button, in overlay or in backpack or in scrollbox. You have only show the function
    for that button not the button himself in UMG.

    Greetz

    • I had this problem, the solution was to ensure that in the AttachActorToComponent nodes, in the SpawnWeaponsAndAssignToSlots function: ensure that Attach Location Type is set to SnaptoTarget, Keep World Scale. ALso, in the SpawnActor Nodes, ensure they are set to Always Spawn, Ignore Collisions.

  20. Hey guys, just a quick note…
    I did the tutorial, and maybe it’s just me but as I saw it the SpawnWeaponsAndAssignToSlots function is done, but never used. That was the thing that prevented me from spawning the weapons.
    I inserted it to the beginning of the EquipWeapon function, so all the weapons are assigned to the slots before equipping. Not really optimized, but it’s working.

    About the models used: The third tutorial began with moving assets from the ShooterGame, so the weapon meshes can be re-used from there.

    Also, I would like to thank ANDRZEJKOLOSKA for the awesome tutorials, they really help in learning for my projects. Keep up the good work!

  21. Problem…

    Everything is EXACTLY the same, but weapons won’t spawn or even play the animations. I think the tutorial is either missing stuff or there is a problem with UE version 4.10.2

    I even downloaded the blueprint, compared, triple-checked everything, still nothing…

  22. Well, i have 99% of this tutorial part done, but im facing now to really strange engine behavior:
    I spawn and set pistol to slot 1, rifle to slot 2 and shotgun to slot 3 but:
    1 – rifle
    2 – pistol
    3 – pistol
    wtf? im done… could someone help me?

    • Well i fixed it, i did mistake (condition != instead of == in showweapon), this is terrible, one mistake and 6 hours for resolving it

      • Hello brother. Sorry to bother you. Can you help me ?
        The weapons doesnt spawn when I press the 1,2 or 3.
        Thanks in advanced.

  23. Hey everyone, i’ve got a problem,

    I manage to spawn the weapons out of the inventory, but here’s the problem:
    The weapons don’t spawn in the players hands. They spawn at the players feet (kinda?) and when the player looks around, the weapons revolve around the player, and adjusting it’s position and rotation to the character. Also, they don’t “move along” with the players hands, so even if they would spawn in the players hands, they would hang still while the players arms play the Idle animation.

    Anyone got any ideas on how to solve this problem?

    • Hi Soundy, I was wondering same thing. I want to create a multiplayer fps and can’t figure out how to solve this problem. Must I spawn the weapon twice? one for fpp mesh and the other for tpp mesh? What about firing? Please if someone has worked in that, let me know
      Thanks

  24. Hi, whilst I appreciate the shear hard work you have put into this tutorial I have to say its one of the hardest I have encountered to follow.

    Using just youtube I have managed half the things on here in a quarter of the time.

    On some of the tutorials you advise to do things later on that should have been done before, its abit of a mess.

    You could have some great things here but its needs to be tidied and explained alittle better
    .

    That’s just my opinion though

    • Yea I’m aware of it 🙂 Stay tuned for free online book that will cover all of the things but with much more explanation which you can see in latest Tutorials.

  25. Hello I’ve got problem, can you help me?
    I can equip my Item but when I click on item in selected weapon the BackpackImage work and go to backpack but in game the weapon does not disappear and when I click on item in backpack this one
    gets over itself.
    thank you for your attention and sorry for my english I’m french.
    bye

  26. guys i found a fix,if anyone having a problem like if you equip pistol,rifle,shotgun and then deselect it from inventory and equip other three guns like sniperrifle,grenade launcher,rocket launcher the mesh wont destroy just go to UI_Debug_WeaponSelection and before spawn weapon and assign slots from here http://i2.wp.com/shootertutorial.com/wp-content/uploads/2015/06/spawnweapons.jpg just before that add a destoy actor and from cast togameplay character get weaepon slot 1,2,3 variable and connect it to destroy actor then connect spawn weapon and assign slot

  27. In “GameplayCharacter” -> “SpawnWeaponsandAssigntoSlots” when I “Break WeaponBackpackItem” the “InSlot” always sends a 0 to the “Switch on Int.” This is causing no weapons to spawn on the character. If anyone has any idea why please let me know.

  28. Somehow I managed to Solve the Weapon not appearing problem… But what the problem I am getting is that it does not spawn pistol and shotgun but only the rifle ….
    And some cannot access function weaponslot_1 error please help……

  29. My weapons wont disappear after you deselect them in inventory, ???
    ps make sure you only use 3 weapons till you get this working (was giving me issues) 0-1-2..

    Email if you have fix or a working copy up to this point… really need to get this for UNI… i well after the initial hurdles of some engine issues 4.8.3 are not the same as 4.8.2 and…. 4.9 only allows the FFPMESH arms from the shootergame package to be imported correctly. other version gave issues..

    mogamingemporium@gmail.com if you have some incite up to this point and the Blueprints you supply have SOO much reconnecting to do due to arrays and Backpack array needing to be replaced .. =(

    Thanks for it to this point i happy to have 1-2-3 working and switching weapons, so i will continue and maybe the removal of them wont be too big of a fix and destroy what i continue onwards..

  30. Anyone getting errors saying their weaponslot_x in SpawnWeaponAndAssigntoSlot is none? I having a hard time figuring out why it isnt geting assigned somthing, where exactly are they getting assigned?

  31. Im having an issue where the weaponslot_x in my SpawnWeaponsAndAssignToSlots function is returning as none, anyone able to figure this out? Im guessing this is the reason why my Weapon doesnt appear.

  32. What character blueprint are you using? GameplayCharacter???? Where did you first create the GameplayCharacter blueprint. I must be missing something here. cannot find GameplayCharacter anywhere inside the FPP starter project or the TPP starter project

  33. I have no idea, might be something with the animations.. but it’s not working, and I contacted the creator of the tutorial series, sadly he doesn’t have time to help me out, so I’m hoping someone out there might have managed to get it working, and can help me out.

    wetransfere link to my entire project: https://we.tl/SfSx7dpqjZ

    Please help me D:

Leave a Reply to mwesten1 Cancel 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.