<?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[Tutorial: How to fire a weapon / create ammo data / impact&nbsp;effects]]></title><type><![CDATA[link]]></type><html><![CDATA[<p><a href="http://shootertutorial.com/2015/06/12/tutorial-how-to-fire-a-weapon-create-ammo-data-impact-effects/"><img class="alignnone wp-image-372 size-full" src="https://shootertutorial.files.wordpress.com/2015/06/shootscreen.jpg?w=641&#038;h=349" alt="shootscreen" width="641" height="349" /></a></p>
<p>In this post I will create shooting functionality for weapons which will be easy to work with later.</p>
<ul>
<li>I want to have couple of ammo types, (plasma, conventional, dum etc) which can be upgraded later,</li>
<li>Aiming should be physically accurate,</li>
<li>There should be impact effects,</li>
</ul>
<p>Let&#8217;s start!</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><b>Creating Ammo Information</b></p>
<p>First let&#8217;s create new ENUM AmmoType and add:</p>
<ul>
<li>Conventional,</li>
<li>Plasma,</li>
<li>Laser,</li>
</ul>
<p>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.</p>
<hr />
<p>Then we need a structure with all data for our ammo.</p>
<p>Create new Structure and name it AmmoData.</p>
<ul>
<li>AmmoType (ENUM AmmoType),</li>
<li>Damage, (float)</li>
<li>CritChance (float)</li>
<li>DamageOverTime, (float)</li>
<li>DamageOverTime_TimeLeft (float)</li>
</ul>
<hr />
<p>Now go to BP_MainWeapon and add those variables:</p>
<ul>
<li>SpreadMin (float),</li>
<li>SpreadMax (float),</li>
<li>SpreadCurrent (float),</li>
<li>AmmoData (AmmoData Structure)</li>
<li>TrailFX (Particle System),</li>
<li>ImpactEffect (ImpactEffect class reference &#8211; we will create this class in a sec!)</li>
</ul>
<p>We need a function to get information where we should spawn projectile or do a line trace (instant fire). Basically getting shoot traces isn&#8217;t so simple and you should read <a href="http://www.moug-portfolio.info/udk-projectiles/" target="_blank">this blog post by Mougli (from UDK) </a>and watch <a href="https://www.youtube.com/watch?v=FwVYXlsTPD4" target="_blank">this tutorial from RATSGAME </a>before moving forward.</p>
<p>Create a function named CalculateShootInformations.</p>
<p><strong>Inputs:</strong></p>
<ul>
<li>Camera (Camera Component),</li>
<li>WeaponMesh (Scene Component)<br />
We don&#8217;t know if our next weapons will be skel meshes so you can use Scene Component here,</li>
<li>WeaponFireSocketName (name)</li>
</ul>
<p><strong>Outputs:</strong></p>
<ul>
<li>ProjectileTransform (Transform),</li>
<li>HitResult (Hit Result structure),</li>
<li>EndLocation (Vector)</li>
</ul>
<p><strong>Local Variables:</strong></p>
<ul>
<li>LocalTransform (Transform),</li>
<li>LocalEndLoc (Vector),</li>
<li>LocalCamera (Camera Component),</li>
<li>LocalWeaponMesh (Scene Component),</li>
<li>LocalWeaponSocketName (name)</li>
</ul>
<p>As you can see the first thing here I&#8217;m assigning inputs to local variables. It&#8217;s good practice if your function will use inputs a lot.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/calculateshootinfo.jpg" target="_blank"><img class="alignnone wp-image-363 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/calculateshootinfo.jpg?w=300&#038;h=160" alt="calculateshootinfo" width="300" height="160" /></a></p>
<p>That&#8217;s all in BP_MainWeapon.</p>
<hr />
<p><strong>Conventional ammo type.</strong></p>
<p>Go to ShooterGameInstance and create new variable named Ammo_Conventional from AmmoData Struct. You can leave variables as 0 for now because we won&#8217;t be dealing any damage.</p>
<p>This will be information about conventional ammo. It can be upgraded later and Pistol will use this data for his AmmoData.</p>
<hr />
<p><strong>Creating Impact Effects</strong></p>
<p>Create new blueprint based on Actor and name it ImpactEffect.</p>
<p>Lot&#8217;s of variables to add here:</p>
<ul>
<li>Defaul FX (Particle System),</li>
<li>Default Sound (Sound Cue),</li>
<li>Default Decal Mat (Material)</li>
<li>Decal Size (float, default 2),</li>
<li>Decal Life Span (float, default 3),</li>
<li>Metal FX (Particle System),</li>
<li>Flesh FX (Particle System),</li>
<li>Glass FX (Particle System),</li>
<li>Concrete FX (Particle System)</li>
<li>Metal Sound (Sound Cue),</li>
<li>Flesh Sound (Sound Cue),</li>
<li>Glass Sound (Sound Cue),</li>
<li>Concrete Sound (Sound Cue),</li>
<li>HitResult (Hit Result, exposed on spawn!),</li>
<li>RandomDecalRotation (Rotator),</li>
</ul>
<p>You should copy Effects and Sounds folders from Shooter Example to your project. If you don&#8217;t know how look <a href="http://shootertutorial.com/2015/06/03/tip-how-to-move-assets-from-examples-different-projects/" target="_blank">here.</a></p>
<p>Assign some defaults to FX and Sounds. Now you need to go to project settings -&gt; physics and add some Surfaces.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/phys-surface.jpg" target="_blank"><img class="alignnone wp-image-364 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/phys-surface.jpg?w=300&#038;h=133" alt="phys surface" width="300" height="133" /></a></p>
<p>In Begin Play of this actor we will spawn sounds and fx on impact.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/impactbeginplay.jpg" target="_blank"><img class="alignnone wp-image-365 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/impactbeginplay.jpg?w=300&#038;h=148" alt="impactbeginplay" width="300" height="148" /></a></p>
<p>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.</p>
<hr />
<p><b>Fire and spawn impact effects!</b></p>
<p>Go to your BP_PistolWeapon (i will use it as example) For ImpactEffect choose ImpactEffect_Pistol.</p>
<p>Now we just need to calculate the fire (function from BP_MainWeapon) and spawn all effects.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/fireeventinpistol.jpg" target="_blank"><img class="alignnone wp-image-366 size-medium" src="https://shootertutorial.files.wordpress.com/2015/06/fireeventinpistol.jpg?w=300&#038;h=88" alt="fireeventinpistol" width="300" height="88" /></a></p>
<p>For TrailFX I&#8217;ve used P_AssaultRifle_Trail from Shooter Example.</p>
<p>To use AmmoData create Begin Play event and set AmmoData from ShooterGameInstance.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/ammodata.jpg"><img class="alignnone size-medium wp-image-369" src="https://shootertutorial.files.wordpress.com/2015/06/ammodata.jpg?w=300&#038;h=75" alt="ammodata" width="300" height="75" /></a></p>
<p>And that&#8217;s all! If you want to have debug crosshair go to GamePlayHud and add this:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/06/crosshair.jpg"><img class="alignnone wp-image-368 size-full" src="https://shootertutorial.files.wordpress.com/2015/06/crosshair.jpg?w=544&#038;h=257" alt="crosshair" width="544" height="257" /></a></p>
<p>Basically Shooter Example is great place to learn keep your time and investigate each functionality there!</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://i2.wp.com/shootertutorial.files.wordpress.com/2015/06/shootscreen.jpg?fit=440%2C330]]></thumbnail_url><thumbnail_height><![CDATA[239]]></thumbnail_height><thumbnail_width><![CDATA[440]]></thumbnail_width></oembed>