Drag and Drop ENEMY AI With Scriptable Objects in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in our games we want a good variety of enemy ai behavior and in this tutorial using scriptable objects i want to show you how you can just make an enemy behavior in your assets folder and then click and drag that into some variable on a script and then get interesting enemy behavior out of it so i have this unity project here and in my game i actually have a player i can move around with the health bar and i have this red square that's going to act as our enemy and right now he doesn't know how to do anything and right now if i walk my player into the square it'll actually do a little bit of damage to me if we take a look at our enemy game object we see we got a rigid body a box collider 2d i have this damage on collision script and it's pretty straightforward just for the sake of this tutorial i'm seeing if we're colliding with a player and if we are well then we're getting the health script and you know dealing some damage and then the enemy also has this movement script which just takes a rigid body a float speed and i have this method called move towards target where we pass in a vector 2 and then we're just telling this game object to move towards that position at the speed and that's it so pretty simple stuff here but again right now our enemy you know it has some components with some functionality but doesn't know how to actually enact ai behavior and we want to design the system in a way where one we can play around with things quickly in the inspector by just plugging and playing with different behaviors and also kind of decouple these things so that wherever our ai behavior lives can kind of just focus in this one component or script or scriptable object so we don't have to really mix our systems up too much and make things coupled and difficult to work with going forward so let's go ahead and make our first script to do this i'm going to make a new c sharp script and i'm going to call this brain it could also be called like ai behavior or something like that and if you watch my power up video we're going to be doing something similar here we can get rid of start and update and what we want to do is actually make a template for all of our ai behaviors going forward and this is going to be a scriptable object so we don't want to inherit from monobehavior we can replace this with scriptable object and because we want this to be more of like a template we actually want this to be a public abstract class and then for simplicity right now we're just going to have one method so i'm going to say public abstract void and we'll call this think this is all we're going to need in this abstract template and so with this abstract template let's go ahead and make our first type of ai behavior we would like to use by implementing this brain template right so we can make a new script and i'm just going to call this chase brain and this is going to end up being kind of simple but it's more just going to show you how you can kind of structure these things so let's go ahead and open the chase brain the first thing we want to do is we don't want to inherit from mana behavior we want to inherit from our just created brain class which is a scriptable object and we want to be able to make this chase brain type in the assets menu in the unity editor so to do that we can in square brackets add a create asset menu tag and then for organizational purposes we'll open a parenthesis and type in menu name and then in quotes i'm going to say brains slash chase and this is just going to organize things in our asset menu kind of nicely and if you're completely lost as to what this create asset menu tag is doing just hang in there i'll show you in a second we can get rid of start and update and you'll see that it's actually complaining because we haven't implemented our abstract method called think which was attached to our brain class so we need to do that and so i can just say public override and then think should come up in my ide if not you can just type this out as is and before we go ahead and implement this i'm actually going to pump the brakes let's first take a look at what this tag appears doing this create asset menu one if we now go into our assets folder and right click and you'll see at the top we have this brains category we can expand and in here we have this chase brain so i can go ahead and create this and in my assets folder i now have this object that i've created which is our scriptable object and i could call this something like player chase and since we didn't actually implement anything in the script yet as expected the inspector window is completely empty so let's actually talk about what we're trying to accomplish with this chase brain script we want to be able to provide some way of identifying a target and then in this think method we more or less want to be able to tell whatever we're plugging this into hey go ahead and chase our target but in order to do that we actually need access to things like a you know like a character controller or a rigid body or something like that right we need a little bit more information and so i'm going to show you how we're going to go ahead in structuring that so before we implement that first brain let's actually take a step back and think about what's actually going to be using these scriptable objects like where are we plugging these brains into well we want to plug it into our enemy and we have two scripts on it right now damage on collision and movement but let's go ahead and make a new one and i'm going to call this new script enemy thinker and this enemy thinker script is more or less going to be the bridge between the scriptable object brains and the body of this enemy and all of its other components so let's go ahead and click and drag our enemy thinker script on and open it up i'm going to get rid of start for now just to make things clear and so this time we actually want to inherit from monobehaviour that's fine because this is going to be a component we attach to our enemy game objects and the first thing we want to do is be able to use one of those brains so we can say public brain brain and then this is going to be really simple what we want to end up doing is calling our brain that think method in our update function here but in order for us to actually use any other information that's stored on our enemy object we want to be able to pass this enemy thinker into our brain so at this point let's actually go back into our abstract brain script and in our think method we'll add a enemy thinker as an argument and now right now this is actually pretty narrow right because it's only for enemies this brain instead of calling this script enemy thinker you could just call it thinker or something like that so it's generic so you can use this on multiple types of objects but this is not a huge deal so now in our chase brain in order for this to compile we need to now also take in a enemy thinker and the error will clear up and then going back to the actual enemy thinker class what we want to do is pass in this and that's it this is all we have to do at this point for our enemy thinker class this is done and so now we can go back to our chase brain and i mentioned before we actually need to provide some way of identifying a target and so there's a ton of ways you could be doing this but for simplicity i'm just going to be using a string so that we can look at things by their tag so at the top here i'll make a variable and i'll say public string target tag so now back in the editor i can click on this player chase brain i made down here in our assets folder and we'll see in the inspector we now have a target tag string and since i called this player chase it kind of makes sense that we would want to put in the player tag as the target but you could really put any tag you wanted on a game object and then just pass that value in here and this isn't ideal because you have to actually manually type in a string as opposed to having like a drop down or something like that with tags but it's not a huge deal i think this is just illustrating the point so now that we have this player target tag that we're going to be passing in how do we actually find our player and tell this script to move towards it so first let's actually get our target and so i'll make a reference to our game object and call it target and then we'll say gameobject.findgameobject with tag and we can use our target tag just like that we can say if we found a target and it didn't come back null then we want to use our enemy thinker to extract our movement component that we know is attached to our enemy game object so in here we could say var movement is equal to thinker dot gameobject dot get component of type movement and then we can say if we found a movement script and it exists on this game object well then we want to say movement move towards which is the single method on that script and we want to pass in our target.transform.position so to recap really quickly we're trying to find a target based on the tag we provided if we found a target then we want to extract a movement component if we have one off of our enemy thinker if we actually have one of those then we want to tell our movement script to move towards that target and the way this is set up if our enemy collides with our player well then it will do damage so at this point if i select our enemy game object we have an enemy thinker script and i can plug this player chase scriptable object into it and when we run the game you'll see that the enemy is now starting to chase us so this is really cool in just like a few lines of code we already set up a scriptable object framework for ai behavior and we're delegating all responsibility of finding the target and chasing it in this one scriptable object called chase brain and in this case i made a player chase scriptable object but i could make another one i could go to create brains chase and call this like scarecrow chase and then i have this scarecrow object here as long as i give this thing a tag so i'll make a scarecrow tag in the target tag i can now write scarecrow and on our enemy we'll drag in the scarecrow chase and this time instead of chasing the player we expect it to chase our scarecrow which it obviously is so you can have this chase a couple different types of objects depending on what you would want in your game and that was just one kind of example right i could make another one called like zombie brain and just like before we want to inherit from brain we want to add a create asset menu tag we want to add an organizational menu listing to it and then we want to override our think method and maybe in this one we just want to move to a random position so we could say var movement equals thinker dot get component of type movement if we have a movement script then we could say movement uh move towards and maybe we'll just say new vector2 of random.range negative 4 to 4 comma random range negative 4 to 4. so this is not a good example but it's just to show you how you can set up a new brain really quick it's just going to pick a random location to move towards and head towards that it doesn't care about the player at all and so i could make a zombie brain and pass that into our enemy and this time when we play the game we see our zombies just kind of walking around so it's just going to mindlessly head towards different locations in the game you're just going to head to a single target when you start the game and so something that would be useful here is to probably put in on this zombie brain scriptable object some sort of timer and basically pick a new location every time you do it and make it relative to where they are so it's not just like a hard-coded world point but you can just have you know a couple different variations of this you can make a brain where your zombie is trying to head towards you or shooting at you maybe you hurt them a little bit and then they know to run away from you what's else is you could actually put like a list of brains or something right and call it brains and then in your editor you now have a list of different ones you could put in so you could put in like player chase and zombie brain and maybe you have one where like you retreat or one where you heal or one where you attack and maybe in here you have some conditional logic like if health is above 75 then chase player you know if health is below 10 run away and you can use the different brains you have in your list here you'll have to set it up in a little bit more of a sophisticated way but using this you have a framework where the main key is you can create these brains on the fly and do kind of cool configurable things with them you have separation of concerns again using this delegate object pattern where the brains kind of worry all about what to do and this enemy thinker thing really just is plug and play and it enables you to kind of have more flexibility when setting these things up in the inspector because you can kind of just toy around with your components and your different types of brains and set some fun values in here and get gameplay up really quickly so i think you guys understand do i think this is the one size fits all solution for enemy ai no i don't do i think it's a really good way of going about it i do and i think it's well worth your time to at least try and see if it's a good fit for your game because it might surprise you so that's it i hope you liked the video and now the last thing i should do is make a new brain that gets you to subscribe i'm sorry [Music]
Info
Channel: BMo
Views: 14,600
Rating: undefined out of 5
Keywords: bmo, unity, tutorial, unity3d, unity2d, bmo unity tutorial, unity tutorial, enemy, artificial intelligence, ai, a.i., enemy ai, unity enemy ai tutorial, enemy a.i. tutorial, unity ai, scriptable, objects, scriptable objects, enemy scriptable objects, enemy scriptable objects ai, enemy scriptable objects ai tutorial, unity scriptable object enemy ai, enemy ai tutorial, enemy brain, ai behavior, unity ai behavior, unity ai behavior tutorial, richard fine, unite
Id: 0VV24g1SxGU
Channel Id: undefined
Length: 12min 25sec (745 seconds)
Published: Wed Mar 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.