Unreal Engine 4 - Enemy Targeting System & Switch Attack Stance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys i hope you're all doing great in today's video we're going to implement a lock-on targeting system and when your character locked onto the target he's going to change his movement stance from normal movement to strafe movement we're going to be locking on to some ai we've been producing in the last few videos but don't worry for anyone out there who's not been following along with my tutorials you can just lock your character onto something else in the meantime before we jump into that an absolutely huge thank you to all my patreons i can't express how grateful i am to you guys for supporting me to create content so thank you for that okay let's dive into this tutorial so guys if you want your character to change his stance to strafe moving after he locks onto the target you will need a strafe blend space and it just so happens i've just done a tutorial on this so quickly hop over to my strafe movement video i'll leave a link in the description and whip yourself up a blend space for those of you who don't know what a blend space is don't worry the other me teaching that tutorial will help you out with that it should only take you a couple of minutes so when you've got your blend space let's set up our character to switch between stances so first let's jump into our third person blueprint create a new bull variable and call this target locked then bring in whatever key press you want to lock onto your target i'm going to use tab off this i'm going to create a flip flop node and then for now all i'm going to do is alt drag in our newly created variable set it to true coming off the first flip flop and set it to false coming off the second so when we press tab the first time we'll set our variable to true then when we press it again we'll set the variable to false and that's what we're going to do in here for now so now let's open up our third person and in blueprint into the event graph pull off our pawn owner and get a reference to the target locked variable we just created now we're going to pull off this promote it to a variable and call it target locked and connect this up to your blueprint line so now we have a target locked variable in our animation blueprint which will change when the one in our third person blueprint does now into the anim graph and into your state machine let's duplicate our idle slash run state and call this idle slash run target locked then we're going to create a transition to it from our idle run state and we're going to transition to this state when our target locked variable is true so open up the transition control drag in your target locked variable and connect it up then let's go back and we're going to transition back to our normal idle slash run state when our target locked is not true so in the transition control drag it in pull off it and bring in a not node and connect it up then go back again and do the final transition from our target locks state to our jumping state and it's going to be if we're in the air so double click to open it up control dragon is in air and connect it up now go back and jump into our target locked state delete the blend space that's there and pull in our strafe blend space from the asset browser in the bottom right now we need to give it two inputs so we already have speed this is implemented in the standard third person template so we can control drag that in and connect it up now we need to create direction so into the event graph of your pawn owner we're gonna get actor rotation and then we're gonna get velocity then off one of these we're gonna bring in a calculate direction and connect the three nodes up then off this we're going to pull off our output promote it to a variable and call it direction when you've done this we're going to go back to our target locked state control drag in our direction and plug it in so what have we got so far we've got a target locked bull in our third person blueprint which changes on a key press this is then passed to the animation blueprint then when this value is true our character transitions into our strafe movement state and when it goes back to false we transition back into our normal movement state awesome now let's do the locking on to our target so into your third person blueprint first we need to get a target to lock onto so we can delete everything coming off our key press that was just temporary then ctrl drag in our target locked variable hold b and click to bring in a branch and connect these up coming off the key press so if we haven't locked onto a target we need to find a target so let's create a sphere collision to do this so bring in a sphere trace for objects node this node basically allows us to generate a collision where we can specify the start location of the collision the end location how wide it is and what type of object we're looking for so let's go down it sequentially for start location we want to start at our character's location so let's bring in a get actor location node and plug this in then for end location we want the collision to end somewhere in front of our camera somewhere in the distance so let's get our follow camera by controlling dragging that in then we're going to pull off this and get its forward vector then we're going to multiply this by a float with a value of 1000 then we're going to add the result onto our actor location with a vector plus vector node then we can plug this into our end location so quite a mouthful i know but don't overthink it this is basically just the maps to get a location in front of something so we're getting a distance one thousand times in front of our camera for now let's throw in a radius of 30 set your drag debug type to persistent and we can test this out and as you can see it's working correctly we've got a collision from our player to somewhere 1000 times in front of our camera cool so back into our blueprint i'm going to up this radius to 300. now for object type we can tell the collision what type of object we're looking for so pull off our object type promote a variable and call it target type compile then add an array element to this then set this array element to whatever you want to lock onto as i'm going to be locking onto ai i'm going to set it to a physics body before we do any more blueprinting let's just triple check our ai is actually set to a physics body so jump into the blueprint of what you want to lock onto i'm going to jump into my ai then for the mesh under collision i'm just going to make sure that it's collision is set to physics actor okay back into our third person blueprint off our sphere trace we're gonna pull off our hit and break this up bring in a print string and bring in a is valid node and connect these up coming off the sphere on our hit results node coming off the hit actor output plug this into our print string and is valid so when we create a collision we're going to print out what we collide with and that is valid if we don't collide with anything will stop us getting error messages remember we've set the sphere to only collide with physics body objects so unless we've got a physics body close to us the result will come out null and give us an error these valid prevents this so off our hit actor again we're going to promote this to a variable and call it lockdown target so if we do hit a target we're going to create a reference to that target then we're going to alt drag in our target locked ball and set it to true bring in a use controller rotation your node we're going to set this to true as well so this node will make our player character face wherever our camera is facing so now when we press our key press if we're not locked onto a character we're going to generate a collision if our collision collides with a physics body we're going to create a reference to it we're going to set target locked to true and we're going to make our character face our camera now if we have a target locked and press tab we want to unlock ourselves so i'll drag in our target locked ball and set it to false then set use controller rotation your node back to false as well and we're going to alt drag in our locked on target by not putting anything in here it's basically clearing it so now that we've got a target to lock onto we need to actually make our camera lock onto that target so bring in an event tick it needs to be a tick as we need to constantly tell our camera to look at that target then ctrl drag in our target locked variable hold b and click to bring in a branch and connect these up coming off the tick now this next part is a lot of maths and instead of wasting a huge amount of your time explaining it all i'm going to ask you to click the link in the description called blueprint nodes this will open up this page and from here you can copy all these nodes the same way you do in unreal by click selecting them and pressing ctrl c then go back into unreal and ctrl v to paste these in your blueprint off the branch is true connect this into the set control rotation node and for the locked on target reference make sure this is the target reference we made earlier the one we generated when we had a collision so as i said i'm not going to explain all the maths but what do you need to know so the set control rotation node is basically setting our screen's rotation and all this mass is doing is generating us a rotation to look at our target based on his location and we're using this r interp 2 node to send our screen to the location over time so it doesn't snap the screen instantly to that spot so we're sending our camera to look at the target's location but if you want the camera to look slightly left right down or up you can change this here just change the value of y and z slightly if you want to look further down subtract more from z if you want to look further up add to z then if you want to look further left add to y and if you want to look further right subtract from y so that is almost everything guys we're generating a target via a collision sphere then we're setting our target location variable to true and this then runs the tick sending our camera to look at that location the last thing we need to do is disable our mouse control when we're locked onto a target so find where your mouse controls are in your blueprint if you're using the third person blueprint you'll have the same as me but it doesn't matter how your controls are set up all we're going to do is add a condition in front of these controls so being clicked twice to bring in two branches and put them in front of the movement controls coming off the false then we're going to control drag in our target locked ball and plug these into both these branches so when our target locks variable changes when we lock onto a target the mouse is no longer going to be able to add any input and that my friends is everything but before you go to anyone unsubscribed it would help me out so much if you could drop a quick sub to my channel i remember when i was learning unreal i'd never subscribed to any other people producing tutorials only now years later i realize how much it helps those people produce content so if this video helps you out it'd be so great if you could support me producing content by dropping a sub anyway guys if you've got any questions drop them into the comments section and i'll do my best to help you out thank you so much for watching and as always i'll see you in the next [Music] video bye
Info
Channel: Beardgames
Views: 36,825
Rating: undefined out of 5
Keywords: ue4 tutorial, learn unreal engine, ue4, blender, game development, indie game development, AI, Dealing Damage, Taking Damage, Damage Animation, Targetting, Strafe Movement, Lock On Target, Epic Games, Unreal Engine 4, Unreal Engine 5, Tutorial, UE5, UE4, unreal engine, Beard games, Beardgames, unreal engine 4, unreal engine 5, epic games, ue5, make games in unreal engine, gamedev, unreal engine 2020, game dev, unreal engine tutorial, unreal, game developer, indie gamedev
Id: TvBnuOm-fDw
Channel Id: undefined
Length: 12min 13sec (733 seconds)
Published: Mon Mar 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.