Unreal Engine 4 - AI Look at & Circle Player (Strafing)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys welcome to another video in this episode we're gonna get the ai to start walking around our character getting ready to attack you can make your ai circle the player or move both ways like mine does i know the animation looks a little bit silly right now but in the next episode we're going to be changing the animation to a strafing animation so this video is actually a continuation from my previous ai tutorials so be sure to check out those before watching this one i'll leave a link in the description so before we do all that fun stuff i want to quickly thank all my patreons for supporting me it really does mean so much to me guys so thank you so much so the first thing i'm going to do is just turn on npc so he's facing our direction and then open up the behavior tree i'm going to disconnect our follow spline path for now and move our chase player over to the left side so we've got our chase player now we need to create another sequence coming off the right side for the ai to move around our character ready to attack and we want our ai to do this when he's in attack range of us so let's go into our blackboard create a new variable as a type bull and call this in player range back into our behavior tree on our sequence let's right click add decorator blackboard call this in range set observer abort to both key query to is not set and the blackboard key to our in player range ball we just made okay cool so now we'll only run these tasks if our in player range ball is false now we need a way to update this ball so we can do this with a service a service is basically just a way of checking and updating values on a reoccurring tick so while our tasks are being run our service is running in the background checking and updating certain values so let's create a service by clicking the service button at the top unlike tasks which require a start and finish node services only require a start node so right click and bring in a event receive activation ai off the controlled pawn bring in a get distance 2 node then bring in a get player character node and plug this into other actor then off the get distance 2 bring in a less than or equals 2 node now let's create two variables one called range as type integer make sure instance editable is checked and one called bb ref as type blackboard make sure instance editable is also checked let's control drag in our range and plug that into our less than or equals to node you may be wondering why we haven't given the range of value but don't worry as we made this instance editable we can do this in our behavior tree then let's control drag in our blackboard ref pull off this and bring in a set blackboard value as bull then plug the less than or equals 2 ball into that value so this service is checking the distance between our ai and our character if the distance between the two is less than our range value our player in range bool we made will go to true if the distance is greater than our range it goes to false into the content browser let's rename this service call it is player in range then back into our behavior tree let's right click our sequence add service is player in range in the details set the range to around 250. this is how close you want the ai to be to our character then set the blackboard ref as our in player range ball so now if we click play our ai will run up to us as soon as he's in range and is player in range is changed to true by our service the ai will no longer chase us so in the behavior tree there's two more things i want to add before we move to the other sequence the first is to create a task to change the ai's movement speed when the ai is circling our character we're going to reduce his movement speed and when he's chasing us we're going to put his movement speed back to its default value so into our content browser let's duplicate our get spline location task and call this set movement speed open up and delete everything but the start and finish nodes and our cast to npc off our cast to npc we're going to get the character movement then off our character movement we're going to set max walk speed pull off our max walk speed and promote this to a variable call this walk speed and set it to instance editable in the details delete our blackboard reference variable as we don't need that then connect up the line compile and go back into our behavior tree bring in our set movement speed task we just made and in the detail section set our walk speed to your ai's normal walk speed mine is 550 so i'm just going to put 550. now one more thing before we move on we want our mpc to constantly be facing us so let's make a task for this in your content browser duplicate any one of your tasks call it rotate to player and open it up delete everything but the start and finish note then off the owner controller bring in a set focus node bring in a get player character node and plug this into the focus then connect the line together so this set focus node quite simply tells the ai controller to rotate the ai to face our player character unlike other nodes set focus doesn't need to be constantly fired so once this is run once our ai will permanently focus on our character until set focus is called again with the new focus so back into the behavior tree bring in our set focus task and connect this into our sequence if you play now you will see your ai rotating towards your character cool we're done with this sequence now let's program the movement in our behavior tree let's duplicate our first sequence and plug it in coming off our selector to prevent any confusion let's call our first sequence chasing player and our second one circling player now let's change some settings for cooldown set it to how many seconds you want your ai to wait between moving around i'm going to set mine to 2. we're going to leave canty player as we only want to run these tasks if our ai can see our player and for in range we're gonna set it to is set so if our ai is in range of our character run these tasks let's duplicate our set movement speed task over to our circling player sequence but this time in the details set the movement speed down to 75 now we need a vector variable to hold the location we want to send our ai to so in our blackboard new vector and call this strafe location back into our behavior tree let's duplicate our send to location task and set the variable to strafe location in the details section so now the last thing we need to do is create a location and i purposefully left this until last as it's quite a bit of maths but don't worry i'll walk you through it so into your content folder duplicate your get player location task and call it get strafe location double click to open it up and delete everything except the set blackboard value and the start and finish notes we can disconnect everything and put our set blackboard and finish node to the side for now so what are we actually trying to achieve here we're trying to make the ai move in a circle around our character so to do this we need to generate a location which will move our ai to the next position in the circle so to get the ai to go from its current location to the next position in the circle we need to move him forward to small amount and then to the right or left a large amount so let's use the power of mass to do this first we need to get our ai's location so off the controlled pawn cast to our ai mine's called npc right click and convert this to a purecast then pull off this and get the mesh then off the mesh get world location so we've got our ai location now we need to add on a forward value so off the controlled pawn let's bring in a get actor forward vector node this finds whatever value is in front of our pawn let's multiply this by a float and put in 50. so now we've created a value 50 times in front of the ai let's add this on to our ai start location so pull off one of your locations and bring in a vector plus vector node now we've generated a location in front of our ai now we need to move it to the right so off the control pawn get actor right vector node control w to duplicate your multiply node but this time put 150 in it now let's add this onto our location add a pin on our existing vector add node and connect it up okay so we've generated a location around here by adding a forward and a right vector onto our original location let's bring in a draw debug line node so we can see this easier put the ai location in the line start and our newly created location in the line end put the duration to 10 and thickness to 5. now we can also connect up our blackboard value to this and send over our generated location to the blackboard so we're still not done here as this will only move our character to the right not left but let's test it out before we move on into our behavior tree bring in our newly created task mine's called get strafe location connect this up in between the set movement speed and send to location make sure in the details section the variable is set to our strafe location variable if your enemy has a weapon equipped make sure to turn off collision so it doesn't start popping our character okay cool that's all working fine now let's make it so our ai can move to the left as well as right so back into our get strafe location task we're going to duplicate everything except the start and finish nodes reconnect the start references into our node and our blackboard into our finished node now for the big reveal how do we change all these nodes so our character can move left instead of right and i'm sure some mathematicians out there have already figured it out we just throw a minus in front of our 150 in the multiply right vector node so it's basically multiplying in the opposite way now to decide if we're going to move left or right we're just going to use a random int node so right click bring in a random int in range node then put in 0 for min 1 for max pull off the return and bring in a equals node then hold b and click to bring in a branch and connect these up together so if the random in regenerate is zero we're going to move right if it's one we're going to move left simple and there it is guys like i said in the next episode we'll be tweaking the animation so our ai actually strafes if you'd like to see that episode this very second head over to my patreon check it out and get that early access and as always guys thank you so much for watching i will see you in the next episode peace [Music] hey
Info
Channel: Beardgames
Views: 19,129
Rating: undefined out of 5
Keywords: Unreal Engine 4 Tutorial, Game Development Tutorial, ue4 tutorial, learn unreal engine, ue4, blender, game development, indie game development, AI, NPC ATTACK, NPC, AI GUARD, 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: 2ethcPt9a3o
Channel Id: undefined
Length: 12min 32sec (752 seconds)
Published: Sat Feb 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.