<?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[Futuristic Soldier &#8211; Behavior&nbsp;Tree]]></title><type><![CDATA[link]]></type><html><![CDATA[<p><span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='640' height='390' src='https://www.youtube.com/embed/yEQa5HHtWyo?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0' allowfullscreen='true'></iframe></span></p>
<p>This will be last enemy for the demo, rest of the characters will be implemented after different environments will be added. I&#8217;m listening you guys and this time I will use Behaviour Tree instead of Blueprints. Some goals:</p>
<ul>
<li>Three types of behaviour: standing, crouching and melee,</li>
<li>Can shoot while running to player,</li>
</ul>
<p>After last enemies you should have enough knowledge to create simple AI by yourself using Blueprints. This time I will focus on Behavior Tree.</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>
<h1>Theory</h1>
<p>If you made all enemies tutorials you should have enough knowledge about AI to move into Behavior Trees. There were long way from <a href="http://shootertutorial.com/2015/06/16/first-enemy-behavior-tree-shooting-at-player/">first enemy tutorial </a>and now I know which features should I use, and how.</p>
<p>Why you should use Behavior Trees instead Blueprints for AI?</p>
<ul>
<li>They run faster than Blueprints,</li>
<li>They were designed for AI and will be improved during UE4 development process,</li>
<li>It&#8217;s easier to debug AI,</li>
<li>It&#8217;s more generic &#8211; you can create tools to speed up AI creation, (tasks, services, decorators)</li>
</ul>
<p>I&#8217;m not the specialist about AI and BT but I really encourage you guys to learn this topic if you want to create games with AI.</p>
<h1>Future Soldier Import</h1>
<p><img class="alignnone" src="https://i0.wp.com/www.3dmodels-textures.com/image/cache/assets/3DModels-Textures/Futuristic%20Soldier351/image/scene01-400x300.jpg" alt="" width="400" height="300" /></p>
<p>For this tutorial I will use <a href="http://www.3dmodels-textures.com/Sci-Fi-characters/FuturisticSoldier" target="_blank">Futuristic Soldier</a>. The pack isn&#8217;t so bad, but it&#8217;s lacking animations. There is an<a href="https://answers.unrealengine.com/questions/276520/importing-skel-mesh-in-different-scale.html" target="_blank"> issue with UE4 about uniform scale and Physical Asset.</a> (btw if you can please vote for this issue maybe Epic will resolve it faster) so you won&#8217;t get ragdoll from this character. I will use death animation instead which isn&#8217;t looking good.</p>
<p>I have imported soldier_anims_separated_LOD0 and they are with gun. You could import mesh without a gun as I did in <a href="http://shootertutorial.com/2015/08/27/enemy-marine/" target="_blank">Marine Tutorial</a>. Thanks to that Futuristic Soldier can lose his weapon on death.</p>
<p>Make sure you are importing the file with:</p>
<ul>
<li>Animated Time,</li>
<li>Uniform Scale: 20,</li>
</ul>
<p>You should get all animations imported separately.</p>
<h1>Preparing needed assets</h1>
<h4>Montages</h4>
<p>Create animation montages form those animations &#8211; leave name by default:</p>
<ul>
<li>soldier_anims_separated_LOD0_Anim_death_Montage</li>
<li>soldier_anims_separated_LOD0_Anim_melee_Montage</li>
<li>soldier_anims_separated_LOD0_Anim_rapidShooting_Montage</li>
<li>soldier_anims_separated_LOD0_Anim_reload_Montage</li>
</ul>
<h4>Blend Spaces</h4>
<p>Create 1D Blend Space named CrouchWalkToIdle. It should go from soldier_anims_separated_LOD0_Anim_crouch to soldier_anims_separated_LOD0_Anim_crouchWalk.</p>
<p>As always in Blend Spaces add some interpolation:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/08/interpolation1.jpg"><img class="alignnone wp-image-1332 size-full" src="https://shootertutorial.files.wordpress.com/2015/08/interpolation1.jpg?w=310&#038;h=160" alt="interpolation" width="310" height="160" /></a></p>
<p>Do this every time doing blend space.</p>
<p>Create another 1D Blend Space named RunToIdle. It should go from soldier_anims_separated_LOD0_Anim_idleWgun to soldier_anims_separated_LOD0_Anim_run.</p>
<p>And last 1D Blend Space named StandWalkToIdle. From soldier_anims_separated_LOD0_Anim_idleWgun to soldier_anims_separated_LOD0_Anim_walk.</p>
<h4>Soldier Type</h4>
<p>Create new Enumeration named FutureSoldierType. It should have 3 values:</p>
<ul>
<li>Crouching,</li>
<li>Standing,</li>
<li>Melee,</li>
</ul>
<p>Using this enum will decide which type of AI / movement enemy should have.</p>
<h4>Socket</h4>
<p>Open skeleton and add new socket to Bind_RightHand bone. Name it S_MuzzleFlash. Here&#8217;s the properties:</p>
<ul>
<li>Relative Location: (X=0.742060,Y=-3.080815,Z=0.147336)</li>
<li>Relative Rotation: (Pitch=0.000000,Yaw=-90.000000,Roll=-90.000000)</li>
<li>Scale: (X=0.100000,Y=0.100000,Z=0.100000)  (yes this is another issue with Uniform Scale, sockets will be big &#8211; I haven&#8217;t posted it on AH yet)</li>
</ul>
<h4>Material</h4>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/material.jpg"><img class="alignnone wp-image-1334 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/material.jpg?w=1024&#038;h=690" alt="material"   /></a></p>
<p>You could create black and white mask for Emissive using Gimp, I have done this different way, but it&#8217;s always better to create masks. I don&#8217;t want to use Gimp or Photoshop at this stage <span class='wp-smiley wp-emoji wp-emoji-smile' title=':)'>:)</span></p>
<h1>Creating enemy Blueprint</h1>
<p>Create new blueprint based on BP_BaseEnemy named BP_Enemy_FutureSoldier. Create new Animation Blueprint from skeleton named FutureSoldier_AnimBP.</p>
<p>Open Character Blueprint and add one ENUM variable (from FutureSoldierType) named SoldierType. It should be editable.</p>
<p>Assign mesh with animation blueprint created couple words earlier <span class='wp-smiley wp-emoji wp-emoji-wink' title=';)'>;)</span> Open event graph and add Die event.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/dieevent.jpg"><img class="alignnone wp-image-1336 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/dieevent.jpg?w=1024&#038;h=318" alt="dieevent"   /></a></p>
<p>It&#8217;s just playing death animation and make sure mesh will be on ground.</p>
<h1>Creating animation blueprint</h1>
<p>Open animation blueprint and add those variables:</p>
<ul>
<li>Speed (float),</li>
<li>Type (ENUM: FutureSoldierType),</li>
<li>LookAtRotation (Rotator),</li>
<li>isDead (bool),</li>
</ul>
<p>Here&#8217;s the graph:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/animbp_graph.jpg"><img class="alignnone wp-image-1338 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/animbp_graph.jpg?w=1024&#038;h=529" alt="animbp_graph"   /></a></p>
<p>As you can see this is just filling the data. I&#8217;m using LookAtRotation instead of LookAtLocation this time because of weird skeleton. In 4.9 there will be possibility to do to LookAtLocation with local/world space, it will help a lot!</p>
<p>Now Animation Graph. Create BasePose from your Blend Spaces:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/basepose.jpg"><img class="alignnone wp-image-1340 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/basepose.jpg?w=1024&#038;h=583" alt="basepose"   /></a></p>
<p>And here&#8217;s the rest of the graph:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/animbp_graph2.jpg"><img class="alignnone wp-image-1341 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/animbp_graph2.jpg?w=1024&#038;h=222" alt="animbp_graph2"   /></a></p>
<p>Should be straightforward for you guys at this stage. That&#8217;s all in animation blueprint.</p>
<h1>Creating Behavior</h1>
<p>Create new blueprint extending from AIController named BP_FutureSoldierController. Open BP_Enemy_FutureSoldier and in defaults panel there is AIControllerClass &#8211; assing BP_FutureSoldierController.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/assigncontroller.jpg"><img class="alignnone wp-image-1342 size-full" src="https://shootertutorial.files.wordpress.com/2015/09/assigncontroller.jpg?w=517&#038;h=113" alt="assigncontroller" width="517" height="113" /></a></p>
<p>This is telling Pawn to be possessed by AIControllerClass. If you want to use BT you need to have AIController assigned to your Character/Pawn.</p>
<p>Now create new Blackboard named SoldierBlackboard. It should contain those variables:</p>
<ul>
<li>isDead (bool),</li>
<li>SoldierType (ENUM, FutureSoldierType)</li>
<li>PawnReference (Object),</li>
</ul>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/blackboard.jpg"><img class="alignnone size-full wp-image-1344" src="https://shootertutorial.files.wordpress.com/2015/09/blackboard.jpg?w=193&#038;h=209" alt="blackboard" width="193" height="209" /></a></p>
<p>Now create new Behavior Tree named FutureSoldier_Standing. Open it and assign Blackboard.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/assignblackboard.jpg"><img class="alignnone wp-image-1345 size-full" src="https://shootertutorial.files.wordpress.com/2015/09/assignblackboard.jpg?w=406&#038;h=123" alt="assignblackboard" width="406" height="123" /></a></p>
<p>Before we move into creating Tree we need to create some helper Blueprints.</p>
<h4>GetPlayerPawn</h4>
<p>Create new blueprint extending from BTTask_BlueprintBase named GetPlayerPawn. Open it and add one variable named PawnReference (need to be Editable!) with Blackboard Key Selector type.</p>
<p>Override Receive Execute AI function.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/addfunctionstotask.jpg"><img class="alignnone wp-image-1347 size-full" src="https://shootertutorial.files.wordpress.com/2015/09/addfunctionstotask.jpg?w=577&#038;h=229" alt="addfunctionstotask" width="577" height="229" /></a></p>
<p>When you deal with Tasks/Services or Decorators you will be Overriding functions.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/getplayerpawn.jpg"><img class="alignnone wp-image-1348 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/getplayerpawn.jpg?w=1024&#038;h=334" alt="getplayerpawn"   /></a></p>
<p>We will use this task in Tree to get the player pawn and assign it to Blackboard. MoveTo will use this data later.</p>
<h4>IsEnemyDead</h4>
<p>Create new blueprint extending from BTDecorator_BlueprintBase named IsEnemyDead. Open it.</p>
<p>Override function Perform Condition Check AI.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/isenemydead.jpg"><img class="alignnone wp-image-1349 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/isenemydead.jpg?w=1024&#038;h=267" alt="isenemydead"   /></a></p>
<p>This is helpful decorator. For example we should stop moving the AI if it&#8217;s dead <span class='wp-smiley wp-emoji wp-emoji-wink' title=';)'>;)</span> Will be used in Behavior Tree.</p>
<h4>MoveForwardToPlayer</h4>
<p>Create new blueprint extending from BTTask_BlueprintBase named MoveForwardToPlayer. Open it and add these variables (all need to be Editable!)</p>
<ul>
<li>DistanceToPlayer (float, default: 300),</li>
<li>RandomDistance? (bool),</li>
<li>MinDistance (float, default: 250),</li>
<li>MaxDistance (float, default: 700),</li>
</ul>
<p>Override Receive Execute:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/movetoplayerexecute.jpg"><img class="alignnone wp-image-1350 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/movetoplayerexecute.jpg?w=1024&#038;h=344" alt="movetoplayerexecute"   /></a></p>
<p>Override Receive Tick AI:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/movetoplayertick.jpg"><img class="alignnone wp-image-1351 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/movetoplayertick.jpg?w=1024&#038;h=346" alt="movetoplayertick"   /></a></p>
<p>This is exactly the same thing I were doing in blueprints in earlier tutorials, but now I can use it on every character &#8211; don&#8217;t need to recreate this again. That&#8217;s power of behavior trees.</p>
<h4>PlayInstanceAnimation</h4>
<p>Create new blueprint extending from BTTask_BlueprintBase named PlayInstanceAnimation. Open it and create these variables (as always they need to be Editable!)</p>
<ul>
<li>Animation ( Anim Montage),</li>
<li>PlayRate (float, default: 1),</li>
<li>MinDistanceToPlayer (float, default: 1000),</li>
</ul>
<p>Override Receive Execute AI:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/playanimexecute.jpg"><img class="alignnone wp-image-1352 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/playanimexecute.jpg?w=1024&#038;h=484" alt="playanimexecute"   /></a></p>
<p>This task is simple &#8211; it is checking if we are in distance to play animation, if yes just play animation as a montage. We will use animations to shoot and deal melee damage. Again this Task can be used on every Character.</p>
<h4>PlayAnimSequenceService</h4>
<p>This don&#8217;t need to be service but I would like to show how they work.</p>
<p>Create new blueprint extending from BTService_BlueprintBase named PlayAnimSequenceService. This will be almost the same node as earlier but it can play couple of animations in sequence.</p>
<p>Open it and create those variables:</p>
<ul>
<li>Animations (Anim Montage, ARRAY, Editable),</li>
<li>CurrentIndex (int, NOT EDITABLE),</li>
<li>AnimInstance (Anim Instance, NOT EDITABLE),</li>
<li>isPlayingAnim (bool), NOT EDITABLE),</li>
<li>AnimRate (float, ARRAY, Editable),</li>
<li>IsDeadBBData (Blackboard Key Selector, Editable),</li>
<li>MinDistanceToPlayer (float, default: 1000, Editable),</li>
</ul>
<p>Create new custom event named PlayAnim.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/playanim.jpg"><img class="alignnone wp-image-1353 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/playanim.jpg?w=1024&#038;h=250" alt="PlayAnim"   /></a></p>
<p>This is something like loop &#8211; passing all Animations and making sure they have been played.</p>
<p>Override Receive Activation AI:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/storeaniminstancevariable.jpg"><img class="alignnone wp-image-1354 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/storeaniminstancevariable.jpg?w=1024&#038;h=227" alt="storeaniminstancevariable"   /></a></p>
<p>It&#8217;s just storing AnimInstance variable for later use. We don&#8217;t need to cast it in Tick.</p>
<p>Now override Receive Tick AI. Services can tick by time:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/tickplaysequence.jpg"><img class="alignnone wp-image-1355 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/tickplaysequence.jpg?w=1024&#038;h=317" alt="tickplaysequence"   /></a></p>
<p>It&#8217;s just calling PlayAnim if can. So basically this service is checking if all animations was played &#8211; if yes &#8211; play them again. It will be used in Behavior Tree.</p>
<h4>GetFutureSoldierData</h4>
<p>This is the last helper &#8211; it&#8217;s strictly connected with Future Soldier enemy. We need to pass rest data to Blackboard.</p>
<p>Create new blueprint extending from BTService_BlueprintBase named GetFutureSoldierData. Add those variables:</p>
<ul>
<li>isDead (Blackboard key selector, editable),</li>
<li>SoldierType (Blackboard key selector, editable),</li>
</ul>
<p>Override Receive Tick AI:</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/getfuturedata.jpg"><img class="alignnone wp-image-1356 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/getfuturedata.jpg?w=1024&#038;h=312" alt="getfuturedata"   /></a></p>
<p>Just set blackboard data.</p>
<h3>Creating Standing Behavior Tree</h3>
<p>Open earlier created FutureSoldier_Standing.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/bt_standing.jpg"><img class="alignnone wp-image-1358 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/bt_standing.jpg?w=1024&#038;h=616" alt="bt_standing"   /></a></p>
<p>So what&#8217;s this doing:</p>
<ul>
<li>Getting Pawn reference on start,</li>
<li>Updating Future Soldier Blackboard in tick,</li>
<li>Trying to play Animation Sequence in tick,</li>
<li>Is Moving Near Player while alive,</li>
</ul>
<p>It&#8217;s really simple AI but you should take your time to debug it and try to make changes. For example you can use Wait node and make him shoot while standing, then walk again.</p>
<p>How to add services and decorators?</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/howtoaddservices.jpg"><img class="alignnone wp-image-1359 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/howtoaddservices.jpg?w=1024&#038;h=563" alt="howtoaddservices"   /></a></p>
<p>Decorators can be add on top of Task, and as can you see I have one isEnemyDead on top MoveTo with Observer Aborts: Self &#8211; which means if false will be returned Move task won&#8217;t be fired.</p>
<p>How to add tasks?</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/howtoaddtasks.jpg"><img class="alignnone wp-image-1360 size-full" src="https://shootertutorial.files.wordpress.com/2015/09/howtoaddtasks.jpg?w=647&#038;h=479" alt="howtoaddtasks" width="647" height="479" /></a></p>
<p>Drag pin from Sequence or Selector.</p>
<p>What&#8217;s the difference between Sequence and Selector? It&#8217;s simple Sequence will stop if ANY node return False and Selector will stop if ANY node return True. Always over your mouse on nodes and check tooltip!</p>
<h3>Creating Melee Behavior Tree</h3>
<p>Create new Behavior Tree named FutureSoldier_Melee, assign earlier created Blackboard.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/melee.jpg"><img class="alignnone wp-image-1362 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/melee.jpg?w=1024&#038;h=703" alt="melee"   /></a></p>
<p>This is different:</p>
<ul>
<li>Move Directly to Player, (using MoveTo Task from Unreal Engine 4),</li>
<li>If near player play anims: Melee -&gt; Shoot -&gt; Melee -&gt; Reload,</li>
</ul>
<p>Again it&#8217;s really simple AI but you can learn a lot from it! Take your time.</p>
<h1>Implementing Shooting</h1>
<p>Your AI is playing shooting animation, but he doesn&#8217;t spawn any projectile.</p>
<p>I will update earlier created Nofity &#8211; Notify_SpawnProjectile to use Distance To Player and spawn muzzle flash effect.</p>
<p>Open it and add these variables:</p>
<ul>
<li>MinDistanceToPlayer (float, default: 1000, editable)</li>
<li>MuzzleFlash (Particle System)</li>
</ul>
<p>And here&#8217;s updated graph &#8211; just check if Enemy is in range of MinDistanceToPlayer before spawning projectile. Spawn Muzzle Flash particle Attached to SocketName.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/spawnprojectileupdate.jpg"><img class="alignnone wp-image-1363 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/spawnprojectileupdate.jpg?w=1024&#038;h=493" alt="spawnprojectileupdate"   /></a></p>
<p>Now in your shooting montages add this notify couple of times, making sure MuzzleFlash projectile is selected and using S_MuzzleFlash socket.</p>
<p>I&#8217;m using earlier created MarineProjectile as Projectile Class.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/addingnotify.jpg"><img class="alignnone wp-image-1364 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/addingnotify.jpg?w=1024&#038;h=545" alt="addingnotify"   /></a></p>
<p>This takes lot of time &#8211; but thanks to Behavior Tree Tools I will be able to create new enemies much more faster than in Blueprints.</p>
<p>That&#8217;s last enemy for environment that I have! Next ones will be implemented for new environment. Now I will focus on main menu and levels system.</p>
<p><a href="https://shootertutorial.files.wordpress.com/2015/09/ue4editor-2015-09-01-18-56-04-008.jpg"><img class="alignnone wp-image-1366 size-large" src="https://shootertutorial.files.wordpress.com/2015/09/ue4editor-2015-09-01-18-56-04-008.jpg?w=1024&#038;h=576" alt="UE4Editor 2015-09-01 18-56-04-008"   /></a></p>
<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 it is taking much more effort!</p></blockquote>
]]></html><thumbnail_url><![CDATA[https://i2.wp.com/shootertutorial.files.wordpress.com/2015/08/ue4editor-2015-09-01-18-56-04-008.jpg?fit=440%2C330]]></thumbnail_url><thumbnail_height><![CDATA[247]]></thumbnail_height><thumbnail_width><![CDATA[440]]></thumbnail_width></oembed>