Enemy Behavior Tree AI FULL IMPLEMENTATION (AI Tree) | AI Series 48 | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Behavior trees these are probably the most prevalent AI Behavior framework in all of game development in this video we're going to implement an enemy AI with four different attacks with a behavior tree so you can see the full process end to end when we have absolutely nothing besides our Behavior tree framework hey Chris here from Mom Academy here to help you yes you make your Game Dev dreams become a reality by giving you a full walkthrough of an enemy AI with a behavior tree let's set a little bit of expectations real fast in this video we're not going to implement a behavior tree ourselves so we're not doing like how to make a behavior tree that's not this video what we are going to be doing is taking a behavior tree framework specifically I'm going to be using a paid one called AI tree that was gifted to me by the developer renowned games it wasn't a perfect experience but I did really enjoy using it and actually after I implemented this whole video and did the whole thing I went and re-implemented this with opposite's behavior designer and I really liked AI tree better you can do these same things with pretty much any Behavior tree framework every Behavior tree Frameworks have a little bit different flavor of how you do things but if you are interested in AI tree after we go through this video I've got a link in the description to that asset and again I had a really great time using it now what we will be doing in this video is implementing an enemy AI a llama that's going to chase our player it'll go idle when the player is too far away it'll spit at the player when it's within some range do two different types of melee attacks and have a gap closer if that sounds extremely familiar to you that's because we did this exact same thing with a finite State machine a couple of weeks to go if you haven't seen that one and you're interested to see the comparison I've got a link to that video in the description as well and just one last thing before we jump in if you're not sure which enemy AI Behavior framework to use I've got a video where I compared by state machines Behavior trees scope and even unstructured AI but I highly recommend you check out if you're not sure which one would best suit your needs and without further Ado let's hop into the implementation let's start off by taking a look at this llama this is a paid asset from these guys I don't know how to say that I'm not even gonna try it comes with quite a few animations this means that we have attacked some different variants of attack a way to walk some Idol animations and a way to roll which is obviously something that we need to have for a llama in the animator we're not having anything hooked up here so we're just going to Crossfade between these different animations whenever we want to move from one state to the next there's really two main components that go into making the behavior tree there's the behavior tree itself and there's what we call the Blackboard where we'll post values there and the behavior tree will read it so in our project panel what we can do is right click create renowned games AI tree Blackboard and also Behavior tree in the same place if you just double click on both of these it opens up two different Windows I'm going to dock those together and also add in a couple other important windows that we're going to want to use as we Define our Behavior Tree on our Blackboard to the top we go to renowned games AI tree Windows node inspector we'll dock that to the side the last one will be key details at least for now every Behavior tree framework has more or less the same components they're just maybe designed a little bit differently if we maximize this so it takes up our whole Space we can see it comes from a root node whenever the behavior tree is being evaluated it's always going to come from the root node and then basically pick from left to right in priority order which state should we go to with AI tree we can press space to get something it works really similar to Shader graph if you ever use that you get nodes with some sub-menus you can choose what you want to add and they're all grouped by what type of thing it is you can also drag from the bottom of this thing and give you that same menu and it'll and I'll just click wait and it'll create that line for you you can delete them as well just by pressing delete so I like to start with the least important thing first so that way we always have something that doesn't really have any conditions to go to which is generally speaking the idle state so from the root we'll want to go down and in our case we're just going to select from a range of things to do so for that it's under Composites it's a selector then we'll have several things beneath it restart with idle come over to the side again Composites sequencer so the selector picks something based on whatever conditions are available sequencer will run things in order the first thing I do in idle state is going to be play an eyeball animation so we can do that with tasks animator Crossfade and the way this one works is it will just Crossfade to that animation state name and you can provide different normal X transition time layers all this kind of stuff I'm not going to worry too much about all these possible parameters we're just going to go straight into playing the idle animation which in my case is called idle underscore a we can rename the name and we'll see it change here once we've played an idle animation we don't want the tree to immediately re-evaluate right because then every frame we're going to come here or every tick we're going to come in here and Crossfade over to play this animation again so what we can do then is wait some period of time in this case let's just say we're going to wait one second just for fun the value we put here should be at least the duration of this animation we're gonna do some back and forth Just to be testing this as we go we select the root node we'll make sure to assign that Blackboard the Llama Blackboard and you can choose how frequently you want this to update we're going to leave it once a frame which is on update we save this we'll minimize it I'm just going to move it off screen we'll bring it back and maximize it in a second we select our llama you need to make sure you have a behavior Runner attached and provide to it the behavior tree you want to execute that's going to be our llama tree and this enemy doesn't have anything in it right now so we'll just disable it so we don't worry about anything else it's just going to be the behavior Runner that's running if we click play lo and behold the idle animation plays we can see because of my wait time isn't perfectly aligned to my animation that he kind of does a double bounce as he Crossfade between them we select the Llama we can also see with the behavior tree as it evaluates if you can kind of see it flicker I hope that comes through on the recording that's whenever the wait time has ended and we're re-evaluating the tree to come to hey we're playing's animation and then moving over to the wait one second cool so we can see exactly what's happening on the tree that's a really nice benefit when we're trying to debug something I'm going to update the wait time to 1.25 seconds and hope we get a little bit less bouncing I guess just all the time Chris know all this stuff and a lot of it comes from experience using unity and doing development in general but I had a really great start in college and that's where I'd like to talk to you about today's video sponsor Southern New Hampshire University or snhu snhu provides low-cost affordable game development and computer science degrees that can help you level up your Game Dev skills maybe you may not know I have a software engineering degree and I feel like it's added a lot of value just going to college getting that degree helped me understand how the software work how do things like State machines work for example and how do video games work which is why I'm partnering with snhu to bring you information about how you can get a college degree from an accredited University with two accredited degree programs that I want to talk to you about today one is a computer science degree which is very similar to the software engineering degree that I have and a game design and game development degree if you're more interested in specializing in game design and development in that degree program you'll learn things like what we talked about on this channel about out how to implement things with gameplay AI physics all these things that we talk about on this channel you'll get structured lectures from University professors who have real world experience you can get either these degrees online through snhu at a really affordable price if you're interested in learning more about these degree programs you can go to snhu.edu lombacademy they'll perform there and a real person will contact you to help you understand how these degree programs can specifically help you again that's snhu.edu lawn Academy go there fill out contact form and someone will contact you with more information about the specific degree program that you're interested in let's come up with the next date of chasing the player that's a good second state in order to chase the player we need to know where the player is that's where the Blackboard is going to come into play you see that we have on the Blackboard Keys self so we know our own transform we can tell it's transform because it's blue we don't have a way to go to the player We'll add that new key to the Blackboard in a second but first let's start designing this state for a lack of a better term to chase a player we'll probably do the same things we just did for idle except move to the player so we'll do another sequencer I'm going to copy paste my animation play the walk animation and then we want to move to the player right so let's go to tasks nav mesh agent move to and here on the Node inspector we can see it's going to do some stuff we can move to a transform key looks like but we don't want to move to ourselves we want to move the player so we can go Blackboard new key transform transform key we'll call the player to see it updates it on blackboard and now on our Blackboard key we can select the player we can have an acceptable radius so this is basically like stopping distance type of stuff and including this so we'll just leave it alone for the time being in the advanced section we have allow partial path track moving goal and go to last point on Lost I'm going to turn off go to the last point on lost so whenever the player leaves range he's just going to stop moving track moving goal will make it so whenever the player moves we will also keep moving that seems pretty straightforward pretty simple but how do we know when we want to chase the player versus whenever we want to be in idle State probably a player needs to be within some acceptable range and if they go too far away we want to give up so again we'll do that with a Blackboard key add a new Bool Boolean and we'll call this one is in Chase range now every Behavior tree framework is a little bit different with AI treat it's really convenient we can right click on a sequencer or one of these types of selector nodes really can be any node to add a decorator or a service a decorator is like a preprocessor that happens before we get to this node and we can do things like compare Blackboard keys do a Blackboard condition a bunch of different stuff for this one we're going to add a Blackboard condition click on that and say if isn't J strange is true then we want to come here the player is not in Chase range then this will fail or be marked as false and we'll come over to this one which has no prerequisites it's just going to execute if nothing else works you'll also notice here we have this flow control this is really important for getting out of weight States and this kind of stuff if we notify the Observer on a value change and if I just say I don't know both here we're going to see that it interrupts both whatever's going down here and it's going to override any lower priority things so for in the middle of this 1.25 second weight and this condition changes we'll interrupt that weight immediately and reevaluate the behavior tree and come in and start playing the wall and moving to the player the same goes four will stop chasing the player if he moves out of range because this is going to get changed to false and in this Behavior tree so far we'll come over and go back to idle you can't interrupt higher priority things because they're higher priority and they're just gonna ignore whatever you're doing I'm gonna turn on the auto save feature so I don't have to forget about saving and this looks great but we don't have a way to set this so far now there are ways to do this we could do some like pre-processing here and do it all in the behavior tree using the built-in perception system from AI tree or using an environment query something like that I think that's going to get a little bit too much in the weeds of how AIT specifically works so maybe we can cover some of that stuff in a future video so what I've done and I don't want to focus too much on the script portion but we are going to review this one because it's really important we made a class called this player sensor and if you recently watched the state machine video this is the exact same script this is a mono Behavior we're going to attach to several objects underneath the llama and all it's doing is raising a events on player enter on player exit whenever the entry Grant or on trigger exit events are raised by the physics system so it's going to have a spear collider that's a trigger that whenever the player comes in it's going to invoke on player enter and whenever the player leaves we'll raise the on player exit event we open up a class called enemy this is going to be where we manage providing data to that Blackboard so we need a few things first is a reference to the player second is going to be a reference to that behavior Runner and then some player sensors for the different ranges I'm going to go ahead and Define all those right now so we'll have three different sensors one for one of the players in Chase range whenever they're in spit range and whenever they're in melee range on start we can hook up the chase sensor callbacks that's home player enter and on player exit respectively to make this a little bit easier to debug we're going to define a bunch of values underneath a header called debug and serialize some Fields like is in Chase range is in spit range and is in melee range we'll set these values whenever we get these events from these sensors and then we'll use these variables to set values on the Blackboard since we're just going to copy paste basically the same thing for all these player sensors I'm going to set them all up right now and then we can use them much more easily in our Behavior tree okay on player exit we'll set his Json range false and on enter we'll set is and Chase range to be true I'm going to repeat this exact same thing for is in spit range and is in melee range with the spin melee sensors so we'll just duplicate that chase sensor on player enter and on player exit twice and anywhere we see Chase in any of these words we're just going to rename it to spit and anywhere we see Chase on the second set we're going to rename it to melee so we'll have melee sensor on player enter melee sensor on player exit and set is in spit range and is in melee range based on which function we're in [Music] we've just done the exact same thing we did on Chase on melee and spit cool now we have some Boolean tracking this kind of stuff probably the easiest way for us to set these Blackboard values is to just do it every frame again it doesn't have to be every frame you could zoom in these events if you wanted it's just probably a little bit easier to understand if we do it on update we'll check the behavior Runners null if it is we'll return because we don't want to do anything we'll get a reference to that Blackboard and we want to set the player position because we need that for the setting position and we'll also set the is in Chase range Boolean we can do if Blackboard try get key player out transform key player key which will return true if it can successfully find that key this is just to safeguard you again setting a key that doesn't exist much like try git component from the unity API it'll set us a valued player key if that's found and we can just do playerkey.set value to be the player transform we do this exact same process for every single key that we want to set so for is in Chase range we can do Blackboard try get keep is in Chase range albuki Chase key tracy.set value is in Chase range great those are the only two that we need right now so as we get to implementing some of the other ones we'll come back and implement the rest of these so now we can see isn't Chase range and player key should get set let's go ahead and move this off the screen again in the unity editor we can enable enemy make sure we hook up our player Behavior Runner and at least the chase sensor and on the player we've attached a player script that has nothing in it it's just a script identifier for the chase sensor we can have this movement sensor that has a sphere collider that is trigger radius of 10 on a layer called enemy sensor some of the stuff was already done because I reused the same project as the enemy State machine video so if you haven't seen that one you're interested in state machines you can go check that out I've got a link in the description if we go to Project settings we go to physics enemy sensors here is set up to only collide with player if you don't have any of these layers yet you can go to tags and layers and add in player enemy sensors roll impact sensor and enemy now and in your physics the very bottom you can configure it like this where enemy sensors only collides as player roll impact sensor collides with default and player and enemy may be equalized with default player and enemy back in the scene view attach player sensor to this movement sensor that just has that sphere collider is triggered true make sure it's on the enemy sensors layer and then hook up that movement sensor to the chase sensor if we click play from here check that out Obama starts immediately coming to me because it's noticed I'm in Chase range on a behavior tree we can see that's evaluating true we're playing The Walking animation moving to the player great okay before I get pushed off the edge let me run away from this llama he's kind of slow so once I get to be about 10 units away we'd expect him to stop moving boom there he goes perfect if we bring back the behavior tree we can now see that it's evaluating Blackboard condition set to false so it's not hitting this it is coming to the sequencer playing that idle animation and then waiting we want to view the Blackboard in real time we go to renowned games AI tree Windows Blackboard viewer stockpad I don't know here as we can see self the player and is in Chase range is currently false foreign move a little bit closer we see is in Chase range flips to true and the Llama starts moving as we run away we'll see if eventually hooks to false and right as that happens he stops moving perfect okay so far so good nothing too complicated just providing values to our Blackboard let's consider a melee attack next we're going to want to do really similar stuff to whatever we just did here so we can control C control V get all that stuff repeated drag it in and instead of playing walk animation we'll play the attack animation we don't want to move anywhere so let's remove that one you'd probably want to play damage or something like that so maybe you do a weight let's put in a value like about half the duration of the animation then you could do something like send a message say that the player should receive the damage and for the value send something like 10. I wasn't planning to do this but let's let's do it at least once just for fun here then we can wait one more time for the other half of the attack animation duration but right now we're checking if it's in Chase range and that's not really what we want we want them to be in melee range so we can go ahead and add a new key to the Blackboard call it is in melee range that's cool if they're in melee range they can attack we can wait the duration of the attack but maybe maybe there's gonna be more of a cooldown than just the attack duration so maybe we need a second Bool we'll call it can attack I like to think about most of the keys most of the time in a Boolean fashion you don't have to do it this way this is just my personal preference you could do something like have on the Blackboard the attack delay and then compare times and do that kind of stuff I just find this to be easier to handle in a script but it doesn't have to be that way so for a Blackboard condition for coming into the attack State we run a Check not if we're in Chase range but if we're in melee range make sure that's true and we need a second Blackboard condition we can add a second decorator Blackboard condition make sure that's on value change board ourselves and lower priority can attack and we want to make sure that's true now it's starting to get a little bit ugly so I'm going to do two things first is create a group call it melee attack drag all the stuff in there do the same for Chase and idle and they have this function called Auto arrange which makes everything really beautiful for you without you having to do anything awesome that looks cool we just now need to set can attack and is in melee range on our enemy didn't get a little bit verbose here right if we have to have four lines of code for setting one value or four vertical lines at least with this formatting so I want to do is make a new private void set Blackboard value that accepts a Blackboard argument string for the key we want to set and then a transform or whatever type we're going to set here and I don't really want to use generics just to keep this a little bit more understandable in case you're not super familiar with those but you could also do this with generics if you were super Advanced so we'll take a transform for this player colleges transform and in there we'll just cut and paste what we had above and refactor a little bit so it sets the value from the argument we receive instead of directly to player transform we'll remove the hard-coded player to use the key that was provided as an argument and rename player key to just transform key then in update we can just call set Blackboard value passing the Blackboard the player key and the player transform so it goes down to one line of code we can repeat this process for the bull key chase key so if we just cut that all if statement duplicate our set Blackboard value and in our new overloaded Blackboard value we can again remove our hard-coded isn't Chase range to accept the key and update our input type from transform to a Boolean finally Chase key dot set value should instead be the bull value that's passed in and Chase key probably doesn't make sense you can rename that to Google key or something like that back in the update we can set the Blackboard value again single line of code passing in is in Chase range and then is in Chase range and since we're providing a Google it's going to go to that second implementation of set Blackboard value it's a lot easier than copy pasting all the ifs and making sure everything's aligned we're just changing two little sections so let's do our attacks we can again do set Blackboard value doing for is in melee range and passing is in melee range same can go for can attack let's actually call this can melee but how do we know we can attack well we're probably going to need to know the last time we attacked and compare that to some kind of attack cooldown so back up at the top maybe we can do an attack configs and normally I would recommend putting this into like a scriptable object but that's going to be maybe too heavyweight for this tutorial so we're going to do basically everything in this one class if you've been watching the AI series for a long time you already know how we would do this if you haven't been watching the AI series for a long time I'll link to some videos that have maybe a more scalable way to approach doing this type of attack configuration put a melee cooldown put it as two seconds with a range from zero to five so we get that little slider in the inspector we also need a last melee time that will just serialize beneath our debug stuff so we can see it in the inspector for can melee we'll just set last melee time plus melee cooldown is less than or equal to time.time then we need to set last melee time when something happens we're going to use the send message functionality we'll just Define a function called on melee and set the last melee time to be time.time in there the last thing in this class at least for now is we're going to want to comment out the spit on player enter and on player exit events because that's actually throwing an error right now we didn't see it earlier because it didn't impact anything but since the melee one comes third we'll actually never reach there because we're going to get a null reference exception when we try to assign these events can we come over to the player class remember that we were sending a message on damage to the player passing in how much damage they were going to take so we need to Define that on this player class let's give ourselves a lot of life so hopefully we don't die we can Define that on damage as a public function that accepts maybe an INT damage and we can just do Health minus equals damage we're not actually going to play or die or go away or anything like that we'll just check out his health in Vector in our Behavior tree then we'll copy the send message node make the receiver be self instead of a player and we'll just call on melee whatever value we send doesn't matter we're not accepting any arguments there since we renamed the parameter in our code to can melee we need to make sure we update the Blackboard to also be using canned melee back over in the scene view we'll first need a melee attack sensor and hard to see this one because it's so small but it's 1.5 unit radius so really close to that llama make sure it is trigger and is on your enemy sensors layer and has that player sensor attached you can drag that one to the melee sensor keep that to cool down and we should be good to go let's check out if that works a little problem here we see is that the Llama is constantly moving probably most time we don't want them to just continually move an attack maybe you do I think it's a little bit weird I think it might be better if he stops to have to attack you select the player we see he's taking a lot of damage great it's on our Chase we probably don't want to come here if we are in melee range for melee range probably close enough we don't care anymore to chase we can probably just go between idle and melee attack so we can do that with a second decorator it's important too that these are evaluated in order I don't know if I said that earlier we're going to want to compare it to false that they are in J's range and we are not in melee range great now if we go back to play I'll see once he gets close enough they'll stop and then attack once I leave the melee range he starts chasing again she's always leaving a little bit of space there so that's great next up I think is everybody's favorite skill a spit attack for a llama we're gonna follow the same pattern of can do something and is in X range then maybe here we'll do something like add a new key so we know we need two bulls first one we'll call is in spit range second one can be canned spit once we see we're starting to get a lot of keys it can be useful to categorize them maybe we can put spit related configs under spit you can put melee related things under melee and then maybe one for like movement that's a little bit more organized I'm gonna go ahead and copy paste the entire melee group because we're gonna do very much similar things to this but instead of isn't melee range is in spit range must be true and maybe can's bit can be true remember that because these are in priority order once one of these evaluates you don't need to care about those lower priority conditions because we're going to interrupt whatever's going on on the right with whatever we're going to do here or rename the group to be spit attack I think the attack animation is the same for spit so wait a second instead of on damaging the player immediately we want to spit out an object right we'll delete that but we will want to do maybe on spit back to ourselves the only difference between these two is we want to instantiate an object so drag one down tasks object instantiate or do we want to instantiate I've made a spit prefab already we'll take a look at this we want to set the position in the rotation so we need to provide those as Blackboard Keys position needs to be a vector 3 key I'll call it like spit spawn position make sure that's in the spit category same for rotation rotation needs to be a quaternion key now we should be able to select those perfect I guess we're gonna give this bit object now this is just a pretty simple amount of behavior that'll Auto destroy itself after some time and move to some Force so that means it requires a rigid body and a collider whenever it gets enabled which we can do on start Instead This is set up so we can use an object pool but we're just going to use the instantiate functionality from AI tree so whenever it's enabled we're going to start a routine to delay disable it we'll create a new weight for seconds if we don't have one wait a frame and then add that rigid body Force going forward from this object that's why it's important that we instantiate it with the correct rotation pointing towards the player or really pointing straight out of the llama's mouth we'll wait some time and then disable ourselves this is going to be a trigger collider so whenever it makes impact with something we're just going to go ahead and make it be inactive you can do something simply like maybe where you damage them for let's say 13 damage if you hit the player here take a look at the spit prefab we're just using some particles from uni particle pack it has a simple rigid body with a capsule collider that is trigger and we have that spit script on it where it'll destroy itself after one second and it's going to move at 100 units of force now to make sure that we have our spit spawning in front of the llama's mouth it's preview the attack animation about I don't know halfway ish here create an empty transform called spit spawn location and position that right around where the spit would come out perfect we'll add a reference to that hook it up in the inspector and set those keys based on this object's World space position and rotation back to our enemy we can do maybe we can add something here like references make a private transform spawn location and on update we can set some Blackboard values that would be a vector 3 and a quaternion which we haven't done this stuff here about setting Blackboard value so let's go ahead and do that twice one for Vector three Vector threes all around and one for a quaternion I just noticed I left this Chase key here for all the variable names so I'll just do a quick refactor of those key names we sent the message on spit so let's define that on spit function we'll need a last spit time instead of last melee time so we'll duplicate the last melee time do the last spit time probably need a bit to cool down as well this is a higher damaging attack maybe it needs a higher cooldown we can finally use the spit sensor so we'll uncomment those now we have set four Blackboard Keys the same two here about is in spit range we can spit and we want to compare the last bit Diamond last spit cooldown to the time we also want to set the vector 3 and the quadrantian keys spit spawn position to b v bits on location position spit spawn rotation [Music] okay so that one seemed like we're doing a lot but we really just repeat it exactly what we just did so let's go through it again a little bit slower to make sure you got it at the top we're now using our spit sensor we added a new spit spawn location we added a new spit spawn cooldown and we added hour of last spit time so we can use that spit cooldown we uncommented our on player enter on player exits so we can properly set is in spit range whenever the player enters and leaves that range we copy pasted some functions here to allow us to set vectors and quaternions in a single line of code and then we just are setting these four Blackboard keys finally we added a new function that would set our last spit time whenever we received this callback at the end of our Behavior tree chain that we use for spitting attacks and just remember that we had already set up spit sensor on player exit and enter to set is in spit range true and false based on when the player enters and exits coming back to the unity editor we can set up a range attack sensor exact same thing we've been doing so far we can see it's about halfway between movement range melee we're getting smaller as we get in so the range attack sensor is the same thing spear glider with his trigger with a player sensor player on the enemy sensor you drag that to our spit sensor make sure we drag our Spit On Location everything else should be good to go we'll click play see what happens the llam is chasing me should be in spit range aha there we go spitting at me should be another five seconds he spits at me again perfect everything seems like it's working great only two more attacks to go and I think the bounce one is going to be very much the same as what we just saw here it's copy based bit this one should be a little bit simpler though we'll call this one bounce attack connect it up remember we need two new conditions well probably only one because for bouncing he's got to be in melee range most likely so let's do one new key call it can bounce this is still a melee attack just a more powerful one so we'll add that here so we need to be in melee range true and can bounce true instead of playing Attack animation it's gonna be a bounce animation we'll wait and then instead of instantiating spit we'll instantiate some bounce particles this is just for some visual effects and we don't want to spawn it so high up it would really be cool if we could spawn it on our location so maybe we need a new position and we can just use the default rotation so we probably need one more key make a new one called self position that's a vector three and just spawn it there so that way it's right there on the ground instead of calling on spit on our message we're going to call on Bounce we're getting very much the same things that we've been doing so far only difference might be here we might again want this on damage event say it does 15 damage Auto arrange this looks a little bit nicer so we'll damage the player for 15 and then we'll say onbounce is complete it looks like we have everything we need here because the particle systems hooked up in the behavior tree all we need to do is set some Blackboard values we already have is in melee range so we'll set can bounce based on our last Bounce Time and bounce cooldown we can Define both of those back up at the top underneath the attack configs we'll make that bounce cool down with the range from 3 to 20 sets the fall to be eight a little bit lower we have all this last spit time we'll add a last bounce time down here we have our callbacks we can make an unbalance and set the last bounce time to be now the last one was we need to set the self position right luckily that's factor three we can just do transform position that one should be it the only thing to keep in mind is our bounce particle systems we're going to want them to be stop action to be destroyed so that way it goes away once it's played because we don't have a script attached to this one it's literally just a particle system it's gonna look like that it's a big bounce let's give it a shot we have to wait at least eight seconds staying within melee range boom perfect that one was pretty easy now that we have basically everything else already in place now last attack is going to be a roll attack which is obviously the most important attack I don't think we need any new stuff here other than the roll configuration so we'll do kenroll like a movement type thing and I don't know we'll just copy paste bounce and see what happens so we don't want to be melee range we're going to be in Chase range it's going to be a gap closing move so we want to be even if we're far away it's fine and we want to be can roll we'll play a roll animation up front and one thing that AI tree doesn't have a node for that I kind of wish they did is the ability to enable an object or enable even a component on an object that doesn't exist here I did give this feedback to the developer and they promised me that they are working on a lot more out of the box standard nodes so I'm sure that's coming in an update soon so we're going to do it with send message say notify roll start on self we're going to say onstart roll argument doesn't matter what we're going to do there is we're going to enable a collider in front of the Llama let's look at that now before we get too far into it there's a roll collider let me enable the mod collider we're going to enable this box collider on Roll start so we're just going to toggle here because this is going to be a Frozen in place rigid body with this impact sensor that's just going to raise events much like what we just saw on the player sensor whenever it makes contact on collision enter it's going to raise an on collision event whenever you receive that we're going to want to exit out of the Roll State we're going to want to stop rolling apply damage whatever we want this to only be enabled when we're actually rolling we don't want to constantly be running into stuff whenever we're moving around because we want to get only events whenever it's actually going to you know make us stop rolling so it should start off with box glider disabled we'll Implement that in just a second so we'll start that we'll enable the collider we'll start rolling from here probably I don't need to wait and instantiate anything probably what we want to do is start moving faster because rolling obviously should pick up speed let's do a sequencer set the speed of the agent to I don't know six that's three times as fast using the navmesh agent move to the player's current position I'm going to not track the moving goal because maybe we shouldn't be that intelligent about how we're moving if we're rolling probably it's harder to do that so we'll calculate some path we'll do turns and all that kind of stuff we just won't continually track the player it's probably too hard to do that if you're rolling well then wait for the full duration of our animation it's gonna be about 33 seconds then we're going to call this on Roll indicating we've successfully completed our role also remember to rename our group roll attack so we don't have two bounce attack labels now that we've set the speed of the agent we're going to want to make sure that we set it back before we start trying to actually move so all the way over wherever we can move let's go ahead and set the speed back to 2 the default speed this one's a little bit more complicated right let's arrange it so we're going to enable collider that's basically what this first one does play the roll animation and then we're going to set the speed try to move to the player and then we're going to wait for some time remember that move 2 doesn't say that it's done until it's actually at that location those weight might be aggressive we'll see how end up looking once we've done all of that stuff so we made it to the location we're going to say okay hey we're done moving which would disable that collider and set the cool down all that kind of stuff whenever we make impact with whatever this enables we're going to want to also make sure that we turn this condition can roll to be false that way we can jump out of here because this aborts itself on value change so we can cancel and jump out of this back to whatever state we should go into once we make impact let's come back to the enemy we need an impact sensor we're also going to need a roll cooldown and we're going to need to know the last time that we rolled on start we need to hook up some events like this but we just have one here and that's on collision so make that we'll call on Roll which we're going to Define here in a second I'm going to move this down to it can live with all these other guys so we're going to set the collider to be enabled false whenever we make contact and we're also going to set the last rule time to be now we're also going to set to be true whenever we start rolling instead of trying to do impact sensor git component I think what I'm going to do is in the impact sensor just have a public collider so we'll just reference impact sensor.collider.enabled equals false in Impact sensor.collider.enabled equals true on Roll start and then in the impact sensor we can just assign that on awake make sure that your on Roll start and the message you sent from the baby tree are named the same in this case what I have on Roll start we need to change to onstart roll now we're good to go here and the last thing we should do is set can roll based on our last roll time and our roll cooldown so if we make impact with something our last roll time is going to immediately get set to now which will set this on the next frame update to be false which will allow us to then from our Behavior tree exit out of this state and that's not the right word for it but exit out of this section of the tree because that decorator is going to start returning false to allow us to evaluate to a different branch of the tree that's what it's called a branch we'll get into a new branch of the tree based on whatever other this stuff gets set to be on our llama we'll hook up that roll collider and that should be the only thing we have to do make sure your box glider is disabled we click play let's see if we can get a rolled happen where he'll impact something in the world about 12 seconds here he comes there he goes and boom came out of the roll and started walking again great all right and this whole time we can see I don't know if I can pull this up at the same time we can see exactly what is this llama doing and why with the visualization of this tree which is one of the main benefits of a behavior tree is we can see exactly what it's doing and how it got there and this was also significantly less code than what we saw when we were doing the state machine because a lot of the code is hidden behind these nodes that we already have that are provided out of the box I know I know it's another long one but I think it was really important to be able to see how you can connect specific implementations of different things like chasing a player together using something like a behavior tree most tutorials including many of my own are focused on a very specific implementation of one thing and then if you're a beginner or even intermediate level developer it can be really challenging to see how do you connect all these things together but this video did help you out and you got value out of it go ahead and like And subscribe to help the channel grow reach more people and add value to more people it really helps me a lot leave me a comment to let me know that you like this kind of video as well now for this level of complexity of AI there wasn't really a big difference between doing a state machine and a behavior tree because they're only really doing one thing right they're trying to kill the player if we start thinking more complex AI like maybe they need to wander around a town interact with different Ai and also be able to kill a player that's where things like scope and behavior trees become a little bit more beneficial over a state machine it become a little bit more easy to maintain but the key difference from what we did today and what we did with the finite State machine run really the key benefit of azure Tooling in general is we remove the programmer from having to do everything we allow somebody who's a game designer and maybe doesn't know how to write code to be able to design the AI behavior and interact in different ways using pre-made things or behaviors that the programmer has put together using Unity hfsm while was totally no problem for me because I was the only one working on it and I was the programmer the designer the everything that wasn't a challenge but then if I bring in a game designer who's going to do way better AI design than I can do they'll have trouble doing that because then we have the pair to do basically anything that's really the benefit of editor tools and I'd really like to thank renowned games for giving me the copy of AI tree today it was really a great pleasure working with it and I can't wait to use the more advanced features like the environment query system that I haven't seen available with any other system and being able to write my own custom nodes I've got a link to AI treat in the description if you thought it was useful and a cool tool to use you can click on that affiliate link down there it'll give me a small little Kickback that really helps me out a lot I've also got links to Humble Bundle if you want to do your asset store shopping or Humble Bundle shopping I'd really appreciate it if you're getting value and you're not in a financial position to be able to support the channel through patreon or YouTube membership you can just do that really helps a lot you are getting a lot of value and you do want to show your support directly through patreon GitHub sponsors or YouTube memberships you can go to patreon.com academy click join here on YouTube go to github.com sponsors slash slumacademy whichever one you choose you're gonna get your name up here on the screen like the phenomenal supporter Andrew Bowen Bruno bozich at the tremendous tier Autumn K Matt Parkin Ivan rulin ifeopolis and solarint and Perry at the awesome tier and all these other great people at the supporter tier thank you all for their support I am so incredibly grateful
Info
Channel: LlamAcademy
Views: 9,322
Rating: undefined out of 5
Keywords: Unity, Tutorial, How to, How to unity, unity how to, llamacademy, llama academy, video game development, development, ai, enemy ai, full implementation, implementation, behavior tree, behaviour tree, behavior, behaviour, tree, unity behavior tree, unity enemy ai behavior tree, unity behaviour tree, unity behavior tree implementation, unity behaviour tree implementation, example behaviour tree, example behavior tree
Id: J7RnUc4g9Pk
Channel Id: undefined
Length: 42min 22sec (2542 seconds)
Published: Thu Aug 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.