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.

This Tutorial has been created using Unreal Engine 4.8.2. 
Make sure you are working on the same version of the engine.

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!

39 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 ?

    Like

    • “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.

      Like

      • 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

        Like

    • 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

      Like

  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.

    Like

    • 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.

      Like

  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

    Like

  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) :) ?

    Like

  8. 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:)

    Like

  9. 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?

    Like

  10. 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!

    Like

  11. 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.

    Like

    • 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.

      Like

  12. 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.

    Like

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

  14. 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

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s