<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Shooter Game Tutorial]]></provider_name><provider_url><![CDATA[http://shootertutorial.com]]></provider_url><author_name><![CDATA[andrzejkoloska]]></author_name><author_url><![CDATA[https://shootertutorial.com/author/andrzejkoloska/]]></author_url><title><![CDATA[How to add weapons &#8211; basics +&nbsp;equipping]]></title><type><![CDATA[link]]></type><html><![CDATA[<p><a href="http://shootertutorial.com/2015/06/08/how-to-add-weapons-basics-equipping/"><img class="alignnone" src="https://i2.wp.com/giant.gfycat.com/MiserlyLikelyEuropeanfiresalamander.gif" alt="" /></a></p>
<p>Now if we have our <a href="http://shootertutorial.com/2015/06/06/how-to-create-inventory/" target="_blank">inventory</a> we can start adding new weapons!</p>
<ul>
<li>Player can choose which weapon he want to play, (for now it will be placeholder UMG we did in <a href="http://shootertutorial.com/2015/06/06/how-to-create-inventory/" target="_blank">inventory </a>tutorial)</li>
<li>Player can change weapon during gameplay with animation, (by keyboard now)</li>
<li>Weapons will attach correctly,</li>
<li>Hands will update idle animation to correctly hold the weapon,</li>
</ul>
<p>We won&#8217;t be doing shooting / reloading in this post. Let&#8217;s focus on basics first.</p>
<p><!--more--></p>
<pre><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.</pre>
<p>For this post I will use assets from Unreal Marketplace. I&#8217;ve chosen <a href="https://www.unrealengine.com/content/b5db9911879141e99f8c34fb14b66691" target="_blank">Military Weapons Silver</a>.</p>
<p><a href="https://www.unrealengine.com/content/b5db9911879141e99f8c34fb14b66691#&amp;gid=1&amp;pid=2" target="_blank"><img class="alignnone" src="https://d31wv1uuljiv5e.cloudfront.net/ue/item/store_EGMMilitaryWeap1_screenshot_4-1920x1080-1a770ea81b9248b7d3cb49515b3bdaa2.png" alt="" width="648" height="364" /></a></p>
<hr />
<p><strong>New Enum: WeaponType</strong></p>
<p>Create new Enum named WeaponType and add: Pistol, Rifle and Shotgun to it. You should know how to add Enums from reading <a href="http://shootertutorial.com/2015/06/02/first-person-look-around-controls-mouse-touch-tilt/" target="_blank">Adding Inputs Tutorial</a>.</p>
<p><strong>BP_BaseWeapon</strong></p>
<p>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.</p>
<p>Create new Blueprint extending from Actor and name it BP_BaseWeapon. Now inside:</p>
<ul>
<li>Create scene component as a root. This is something that I&#8217;m always doing when creating new blueprint,</li>
<li>Create Skeletal Mesh Component and name it WeaponMesh,</li>
<li>New variable: IndexInBackpack (int) which should be Editable and Exposed On Spawn,<br />
We will be passing this from Backpack_Weapons in ShooterGameInstance,</li>
<li>New variable: AttachSocketName_FPP (name) default value: WeaponPoint</li>
<li>New variable: AttachSocketName_TPP (name) default value: WeaponPoint<br />
Those will be used to attach the weapon to FPP or TPP meshes.</li>
<li>New variable: WeaponType (enum WeaponType)<br />
This will hold information for animation blueprint. Hands need to update animations for different weapon types.</li>
</ul>
<p>For now it&#8217;s it. Later this blueprint will have variables like CurrentAmmo, MaxAmmo etc.</p>
<hr />
<p><strong>Updating WeaponBackpackItem structure.</strong></p>
<p>Before we go further we need to update WeaponBackpackItem structure we had created <a href="http://shootertutorial.com/2015/06/06/how-to-create-inventory/" target="_blank">earlier</a>. WeaponToSpawn shouldn&#8217;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.<br />
<img class="alignnone wp-image-280 size-full" src="https://shootertutorial.files.wordpress.com/2015/06/weapontospawnclass.jpg?w=575&#038;h=32" alt="weapontospawnclass" width="575" height="32" /></p>
<blockquote><p>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.</p></blockquote>
<hr />
<p><strong>BP_Weapon_Pistol</strong></p>
<p>Create new blueprint based on BP_BaseWeapon. Name it BP_Weapon_Pistol.</p>
<ul>
<li>In Components tab select WeaponMesh and chose Pistols_A skeletal mesh from <a href="https://www.unrealengine.com/content/b5db9911879141e99f8c34fb14b66691" target="_blank">Military Weapons Silver</a>,</li>
<li>Now you won&#8217;t see inherited variables from BP_BaseWeapon. To see them you need to click on small eye <span class='wp-smiley wp-emoji wp-emoji-wink' title=';)'>;)</span><br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/showinheritedvariables.jpg" target="_blank"><img class="alignnone wp-image-290 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/showinheritedvariables.jpg?w=300&#038;h=117" alt="showinheritedvariables" width="300" height="117" /></a></li>
<li>After that you will be able to see variables created in BP_BaseWeapon and you can overwrite them!</li>
<li>WeaponType should be Pistol,</li>
<li>AttachSocketName_FPP should be S_PistolSocket</li>
</ul>
<p>That&#8217;s all here, let&#8217;s create another weapon.</p>
<p><strong>BP_Weapon_AssaultRifle</strong></p>
<p>Again create new blueprint based on BP_BaseWeapon. Name it BP_Weapon_AssaultRifle.</p>
<ul>
<li>WeaponMesh should be Assault_Rifle_A,</li>
<li>WeaponType should be Rifle,</li>
<li>AttachSocketName_FPP should be S_AssaultRifle,</li>
</ul>
<p><strong>BP_Weapon_Shotgun</strong></p>
<p>The same thing as above. WeaponType should be Shotgun, AttachSocketName_FPP should be S_Shotgun and WeaponMesh Shotgun_A.</p>
<p>That&#8217;s it. We have three weapons now!</p>
<hr />
<p><strong>Sockets</strong></p>
<p>Open Hero_FPP_Skeleton.</p>
<p>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. <a href="https://docs.unrealengine.com/latest/INT/Engine/Content/Types/SkeletalMeshes/Sockets/index.html" target="_blank">This documentation from Epic</a> is ideal place to learn about creating sockets and previewing meshes. I won&#8217;t be posting about how to create sockets &#8211; please read documentation.</p>
<p>Create these sockets from b_RightWeapon bone:</p>
<ul>
<li>S_Shotgun:<br />
&#8211; Relative Location: (X=0.299006,Y=1.298091,Z=8.979076)<br />
&#8211; Relative Rotation: (Pitch=85.000000,Yaw=90.000000,Roll=0.000000)<br />
&#8211; Relative Scale: (X=1.200000,Y=1.200000,Z=1.200000)</li>
<li>S_AssaultRifle:<br />
&#8211; Relative Location: (X=0.299006,Y=0.528186,Z=9.127792)<br />
&#8211; Relative Rotation: (Pitch=85.000000,Yaw=90.000000,Roll=0.000000)<br />
&#8211; Default Relative Scale 1,1,1</li>
<li>S_PistolSocket:<br />
&#8211; Relative Location: (X=0.714007,Y=1.284835,Z=11.106700)<br />
&#8211; Relative Rotation: (Pitch=85.000000,Yaw=90.000000,Roll=0.000000)<br />
&#8211; Relative Scale: (X=1.100000,Y=1.100000,Z=1.100000)</li>
</ul>
<p>If you are previewing your meshes in sockets don&#8217;t be afraid that they won&#8217;t fit exactly to hands. We will be using AnimationBlueprint to fix that.</p>
<hr />
<p><strong>AnimationBlueprint</strong></p>
<p>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. <a href="https://docs.unrealengine.com/latest/INT/Engine/Animation/AnimBlueprints/index.html" target="_blank">Please read Epic&#8217;s documentation</a> before moving forward.</p>
<p>Open HeroFPP_AnimatoinBlueprint and create new variables:</p>
<ul>
<li>BaseRotation &#8211; rotator default value: (Pitch=0.000000,Yaw=0.000000,Roll=-6.681010)</li>
<li>PullDownPercentage &#8211; float, default 0</li>
<li>CurrentWeaponType &#8211; WeaponType Enum,</li>
</ul>
<p>We will be assigning those variables in Event Graph.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/animbpeventgraph.jpg" target="_blank"><img class="alignnone wp-image-292 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/animbpeventgraph.jpg?w=300&#038;h=55" alt="animbpeventgraph" width="300" height="55" /></a></p>
<p>Now go to Animation Graph. Create something like this:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/firstpart.jpg" target="_blank"><img class="alignnone wp-image-294 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/firstpart.jpg?w=300&#038;h=166" alt="firstpart" width="300" height="166" /></a></p>
<ul>
<li>Its taking FPP_LauncherAim animation,</li>
<li>Then it&#8217;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 <a href="https://www.youtube.com/watch?v=pfKdr0FRC5g&amp;index=7&amp;list=PLZlv_N0_O1gZS5HylO_368myr-Kg2ZLwb" target="_blank">this</a> tutorial form Epic. If you have time you should watch the full playlist. <a href="https://docs.unrealengine.com/latest/INT/Engine/Animation/AnimMontage/index.html" target="_blank">Montages documentation can be found here.</a></li>
<li>Then it&#8217;s adding rotation to RightHand bone using BaseRotation variable,  (without that our gun will be pointing up)</li>
<li>After that it&#8217;s modifying b_Spline1 bone using Alpha (from 0 to 1) &#8211; it&#8217;s used to control equipping animation,</li>
<li>Then it&#8217;s caching the pose to new cache called BasePose so we can use it later.</li>
</ul>
<p>Now we need to blend pose by Enum &#8211; by our WeaponType.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/blendposebyenum.jpg" target="_blank"><img class="alignnone wp-image-295 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/blendposebyenum.jpg?w=295&#038;h=300" alt="blendposebyenum" width="295" height="300" /></a></p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/blendposebyenumfinalanimation.jpg" target="_blank"><img class="alignnone wp-image-296 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/blendposebyenumfinalanimation.jpg?w=300&#038;h=111" alt="blendposebyenumfinalanimation" width="300" height="111" /></a></p>
<p>You will be able to fill all of the TransformBone blocks but there is a new block called <a href="https://docs.unrealengine.com/latest/INT/Engine/Animation/IKSetups/index.html" target="_blank">TwoBoneIK</a>.</p>
<p>It should look like this:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/twoboneik.jpg" target="_blank"><img class="alignnone wp-image-297 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/twoboneik.jpg?w=292&#038;h=300" alt="twoboneIK" width="292" height="300" /></a></p>
<p>Basically what it&#8217;s doing it&#8217;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!</p>
<p><img class="alignnone" src="https://i0.wp.com/giant.gfycat.com/EnchantedDrearyAtlanticspadefish.gif" alt="" /></p>
<p>I&#8217;m using it to change left hand location to hold the weapon. TwoBoneIK is powerful and you should learn it!</p>
<p>Here you can find the whole animation blueprint.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/animblueprint.jpg" target="_blank"><img class="alignnone wp-image-293 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/animblueprint.jpg?w=300&#038;h=142" alt="animblueprint" width="300" height="142" /></a></p>
<p>That&#8217;s all in Animation Blueprint!</p>
<hr />
<p><strong>Preparing GameplayCharacter</strong></p>
<p>Gameplay Character will be responsible for spawning weapons, attaching them to slots and changing them. Open GameplayCharacter and add some variables:</p>
<ul>
<li>WeaponSlot_1 (BP_BaseWeapon)</li>
<li>WeaponSlot_2 (as above)</li>
<li>WeaponSlot_3 (as above)<br />
Here we will store references to weapons that we have chosen in Inventory before gameplay,</li>
<li>CurrentWeapon (BP_BaseWeapon)<br />
This will hold current weapon reference so we can check which weapon player is holding,</li>
<li>isReloading (bool)<br />
This will tell us if hands are doing reloading animation,</li>
<li>isChangingWeapon (bool)<br />
We need to know if we are currently changing weapon,</li>
<li>WeaponPullDownPercent (float &#8211; default 0)<br />
This will be used for put weapon down and up as a changing weapon animation. We don&#8217;t have any change weapon animation so we need to create one,</li>
</ul>
<p>After variables let&#8217;s move to functions.</p>
<ul>
<li>GetShooterGameInstance &#8211; function like in other classes we have created, pure function which will return ShooterGameInstance,</li>
<li>SpawnWeaponsAndAssignToSlots<br />
This is important function. It will spawn meshes and plug them to WeaponSlot_X<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/spawnweaponsandassigntoslots.jpg"><img class="alignnone wp-image-284 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/spawnweaponsandassigntoslots.jpg?w=300&#038;h=121" alt="spawnweaponsandassigntoslots" width="300" height="121" /></a></li>
<li>ShowWeapon &#8211; input &#8220;BP_BaseWeapon&#8221; named &#8220;WeaponToShow&#8221;<br />
This is helper function to hide and show weapons in slots.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/showweapon.jpg" target="_blank"><img class="alignnone wp-image-285 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/showweapon.jpg?w=300&#038;h=159" alt="showweapon" width="300" height="159" /></a></li>
</ul>
<p>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.</p>
<p>Create new custom event &#8220;Equip Weapon&#8221; with one input &#8220;BP_BaseWeapon&#8221; named &#8220;Which Weapon&#8221;<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/equipweapon.jpg" target="_blank"><img class="alignnone wp-image-286 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/equipweapon.jpg?w=300&#038;h=112" alt="equipweapon" width="300" height="112" /></a></p>
<p>We are using Timeline. If you don&#8217;t know what&#8217;s Timeline please read <a href="https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Timelines/index.html" target="_blank">Epic documentation</a>.</p>
<p>Our timeline is driving float variable and a event.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/equipweapontimeline.jpg" target="_blank"><img class="alignnone wp-image-287 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/equipweapontimeline.jpg?w=300&#038;h=135" alt="equipweapontimeline" width="300" height="135" /></a></p>
<p>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.</p>
<p>The last thing in Event Graph is to create key bindings for 1,2,3 and fire EquipWeapon event.<br />
<a href="https://shootertutorial.files.wordpress.com/2015/06/keybindings.jpg" target="_blank"><img class="alignnone wp-image-289 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/keybindings.jpg?w=240&#038;h=300" alt="keybindings" width="240" height="300" /></a></p>
<p>That&#8217;s all in GameplayCharacter!</p>
<p>Now if you want to test this out just add a button to UI_Debug_WeaponSelection like this one:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/spawnweapons.jpg" target="_blank"><img class="alignnone wp-image-299 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/spawnweapons.jpg?w=300&#038;h=59" alt="spawnweapons" width="300" height="59" /></a></p>
<p>Take your time to learn animation blueprints and object oriented blueprinting.</p>
<hr />
<blockquote><p>Creating ShooterTutorial takes a lot of my free time.<br />
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=8UXM3JZGDPPPE"><img src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" alt="Buy Now Button" /><br />
</a>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.</p>
<p>Implementing game is taking time but writing about this is taking much more effort!</p></blockquote>
]]></html><thumbnail_url><![CDATA[https://i1.wp.com/shootertutorial.files.wordpress.com/2015/06/spawnweapons.jpg?fit=440%2C330]]></thumbnail_url><thumbnail_height><![CDATA[86]]></thumbnail_height><thumbnail_width><![CDATA[440]]></thumbnail_width></oembed>