Tic Toxic Studios - AI Controlled NPCs in Unreal Engine 5 - Part 1.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's get started with some beginner AI for non-player characters or commonly known NPCs if you are new to behavior trees or looking to get extra tips and ideas this video is for you in this part we will be making an AI controlled NPC that walks around in a couple different ways we will cover setting up the character with an AI controller a Blackboard and behavior treats to make decisions if you find this useful like And subscribe leave a comment about questions or what you want to see in the future first we will create a new project and adjust my layout so you can see everything that I'm doing a new folder to keep things tidy and make a new blueprint class for the AI NPC set the animation class and skeletal mesh to your desired classes I will be using Quinn for this example move the mesh into position and rotate it to face forward if you forget to check use acceleration for paths your NPC will slide instead of walk to set up the AI controller create a new blueprint class and search for the AI controller parent in the highest level of your NPC blueprint set the AI controller class to the new AI controller blueprint you just made and change autop possess AI to place in world or spawn depending on how you plan to use it in the AI controller blueprint let's explore a quick overview of what you can do here this blueprint runs when the AI possesses and takes control of your NPC so you can run just about anything in this event graph I'll demonstrate by having it print something as soon as it takes control you can see the text on the screen as soon as I hit play the cool thing here is that each instance of the NPC is separate from the others so in tick we can tell the NPC to display its name we'll make some copies and you can see that each instance displays its name in every frame let's set up a Blackboard and behavior tree I like to do this by creating a behavior tree first find this under the artificial intelligence heading in the rightclick menu in the Behavior tree select new Blackboard in the upper left and verify that it's set as the Blackboard asset for that behavior tree you can quickly switch between the behavior tree and the Blackboard with the buttons in the top right corner to keep it simple the Blackboard just gives you a place to store variables called keys you may add as many as you need these keys can be set to whatever type you want or need I'll create a few let's make some locations as vectors and for something different I'll make a key for for an object a Target and a bull if we have a Target we won't be using all of these examples in this part these keys are only used by the controlling MPC by default if you want them to share a key you can check instant sync and if you update that key it changes all the instances of the NPC's keys this is useful if you have something like a horde of zombies chasing a player if one knows they all know let's talk about Behavior trees now to have your behavior tree run you just have to tell the AI controller to run it at the play now let's step back and take a look at how Behavior trees operate there are three types of composite nodes in unreal five each Act in a unique way in this part we will only be discussing two of them sequence nodes only pass if all the tasks below it pass the tree always performs the operations left to right let's look at this fantastic artwork to explain behavior trees always start at the route and the route must go to a composite node here it goes to a sequence node that has three tasks attached to it the operation runs down the left side to get to task one let's say task one passes it then runs task two task two passes and runs task three task three reports success and the sequence node is considered successful let's say task two is going to fail task one will run and pass task two will run but hits a failure and reports unsuccessful this causes the sequence node to fail instantly and not run task 3 at all let's say the sequence node has two sequence nodes beneath it each with three tasks if each sequence passes all the tasks will be performed now let's say that task two fails again task three won't run since the sequence node it's attached to now fails it won't run the other nodes either if task four fails everything after that fails also selector nodes act differently we'll replace the topmost node with a selector node let's say all the tasks pass here once a selector node gets a pass any success at all it will not run anything after that it will try everything else until it gets a success and will only fail if everything fails back in the editor let's add a sequence node and just a default weight node to watch it run you can see that it runs the weight node and just does it again in a loop let's make our our own action called the task click new task at the top left of the behavior tree and name it my task will pick a random point the MPC can get to and move there so I will call it Patrol random Point start AI tasks with receive execute AI to start running the task as soon as the behavior tree gets this task I will use the node get random location navigable radius and use the actor current location for the origin set the radius so it will pick within whatever distance you want I used 1,000 units then add AI move two and set the random location result to the destination pin add the task to the behavior tree I want the NPC to walk then wait so I added the patrol task to the left of the weight task you can see when I hit play my MPC is not walking even though the behavior tree is running of course I didn't say where he was allowed to walk add a nav mesh bounce volume to your level and expand the Box brush to cover the area you want to be able to walk press p on the keyboard to verify it's working green means it's walkable for the NPC ah there he goes wait now he's only moving one time oh you can see that the behavior tree just stops on the random patrol task I didn't give it any way to say if it passed or failed the Finish execute node fixes [Music] us there it works now but it's a patrol not a Sprint let's change how fast he can move a cast to get the movement component and set the walk speed to something lower much better what happens if I switch the random patrol and the weight task you see the same things happen but it waits first and then it moves I can move some panels around so that you can see the behavior tree and actions together let's add a few more and watch them you can also pick what NPC instance you want to watch I don't like that they all walk at the same speed let's randomize that a bit and give some very iation to the NPCs let's start using some of those Blackboard keys that we set up earlier we'll have the NPC blueprint come up with a random walk speed and send that value to the Blackboard we'll have to create a new key to hold it we must tell the editor what kind of value we plan to set and what key we are setting there are other ways to do this but make literal name is the easiest to explain here I'll show other ways to work with Blackboard keys in a future video the key name must match exactly to the name in the blacko board watch for spaces and capital letters I usually copy and paste to avoid mistakes in the random patrol task we will get the value from the Blackboard and use it to set the max walk speed I'm not convinced it worked they kind of all still look the same let's see what the speeds are yeah they are different but too similar I'll really spread out that range to see it work okay it's working I can shrink that range again I really don't want my NPCs wandering in any which way they please at least not all the time I want them to go to a place of my choice let's make a new task called Patrol to set point we'll finally make use of the start location and Patrol location Keys now so we'll need to set them to the Blackboard in the NPC Again Play collapse to a function to keep it neat and set the starting values like before but how do I know where to patrol to I could manually type a location but that would be a lot of work each time I added a new NPC to the level let's create a component in the NPC to Mark A Place For Us add a static mesh sphere and scale it down I called it Patrol point and made it a brighter color to see better and made it hidden in game uhoh don't forget to turn off can effect navigation or the NPCs can't ever get to it much better now by selecting an NPC and going to that Patrol component we just made you can drag it around to where you want the NPC to walk to now we can save that location and store it to the Blackboard like we did before make sure that the key key names are all correct now on the patrol to set point task get those values from the Blackboard for use I like to save them as a local variable to make it easier to get them later tell the MPC to move there and add it to the behavior tree oh yeah they're running again copy the walk Speed code from the other task and add it in allow the task to say if it's finished correctly and test it as you can see with how the behavior tree is set it will run the Patrol to set point then random patrol then wait also once the MPC is at the patrol point we want it to go back to the start Point let's add a key to tell if the MPC should be going back to the start or not Blackboard Boolean values default to false we can branch on if we are returning the false pin is what we've already coded we just have to change the value when we get there if you are returning we move to the start point and change change the value of return to [Music] false you can watch the task blueprints to see what code is currently running we were buve I think that we all enough what if I don't want them to patrol to a set point what if I want them to be Rand again plus I have perfectly good code in my patrol random task that I kind of want to use let's add a public variable to our NPC class to decide if we are random or if we walk to a set point perfect time to talk about decorators I find the best use of decorators is to gatekeep the task before it runs and decides if it even should run decorators cause the attached task to pass or fail before the task runs to see if the MPC should Patrol to a set or random point we will perform condition check Ai and just get the patrol random variable from the MPC class and return it this error is what happens when you add a variable but fail to compile you can see that The Decorator causes the whole sequence to fail to fix this we can switch to a selector now if the first task fails because random patrol variable is false it will try the next one we still want to run the weight tab at the end so we add a sequence node to run the weight task after one of the patrol tasks succeed you can invert decorator results as needed and that's the end of part one you should now have an AI controlled NPC that can either Patrol randomly or to a set point M back using Behavior trees a lot of information was packed in here if it was too fast or unclear let me know and I'll break down that part better hope this helps with whatever problem brought you here or your fresh journey into game development thank you for [Music] watching
Info
Channel: Tic Toxic Studios
Views: 494
Rating: undefined out of 5
Keywords: UE5, unreal engine 5, gamedevelopment, gamephysics, gametutorial, gamedevtutorials, howtotutorial, npc, tutorial, animation, gamer videos, ai, artificial intelligence, NPC, behavior trees, blackboards, video game AI, Tic Toxic
Id: Ez2xf0SkI5M
Channel Id: undefined
Length: 13min 8sec (788 seconds)
Published: Wed Feb 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.