How to BUILD and LAUNCH an RTS Game in UNITY - ep5. Enemy AI (Part1: Aggro) ft NavMeshAgent

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome back to the how to build an rts game from start to finish series where we learn how to build a game from the ground up while completely at the mercy of you guys [Music] hey guys so in our last video we decided to add a little bit more functionality to our units as well as we learned a little bit about customizing our inspector if you haven't seen that one yet you should check it out as we're going to be using the same project as we did before and i'll go ahead and link that in the card and in the description below this time we're going to clean up a little bit on how we are actually accessing our unit stats as well as making our enemy aggro when we walk too close and he will begin to chase us to start attacking us so the first thing we need to do to start cleaning things up just a little bit is we are going to open our unit handler script and we're going to kind of go through each script and we're going to change up our singletons just a little bit so we can start accessing functions at the correct time and initializing things properly because start is actually one of the last things that gets called but whenever the game is starting so we want our instance to be called before anything in the start function initializes so to do that we are going to say private void void awake now the reason we're using awake is because awake happens before start so now the instance will always be active meaning if something has to reference this script it'll already be active and it can reference it before this they can reference it in their start menu and will already be active because we activated and awake so we need to make this happen as well in our input handler let's see so we scroll to the top here private void awake oops okay and we're going to move this i'm also going to add private here don't really need to but it makes me feel more comfortable having things uniform and now we also need to do this in our player manager i believe yes okay so private void awake and we're going to move our instance into awake i'll make this private as well and make this private as well great now let's go back into our unit unit handler and let's just clean up this function just a little bit so the first thing that i want to do is actually make a reference to our player units and our enemy units so up here we are going to set a transform and we'll call it p units transform p units and we want to access our player manager here right so we're going to make it so we don't have to do these extra steps so let's head up here to the top i'm going to say using lp.fdg.player okay now we can actually come down here and just say using player manager dot instance dot player units now we'll do the same thing um but we're going to make it for our enemy unit so we'll just copy this and i'll change this to e units and this will actually be enemy units there now in our if statement we can remove all of this and just say if it's equal to p units and this will just say e-units now before we can actually access our enemy stats we need to make our first enemy ai script so let's go back into unity it's going to compile a little bit here and then we'll create a new c-sharp script we're going to call this enemy unit okay now let's go ahead and open this back up into visual studio we need to make our namespace so i'm going to put namespace uh lp dot fdg um we are messing with units and this time they're not the player units they're enemy units and um enemies okay oops now we'll remove all of this and place it inside i'm going to get rid of everything inside of this as well we also do want to add using unity engine dot ai this will allow us to access our ai functions so first thing we want to do is require and we're going to use a type of nav mesh agent so again this just places a nav mesh agent onto our object if it's not already there okay now in our player unit we set our stats this way and i actually think that there's going to be a lot better way of handling this and it's going to make it cleaner and easier to build on in the future so in order to do that let's head back over into unity and we're going to create another script and we're going to call this unit stat types okay then we'll open this up in visual studio now let's add our namespace so again we're going to say namespace lp fdg and we're um editing units right so we'll remove this place it on the inside now we want to remove mono behavior because we're going to make this a scriptable object and now we're going to remove everything from the inside and inside of this class we're going to create another class called base because they'll be our base stats okay now this is where we'll actually store those stats so we're going to do a public float and i'm changing this from int to float because we'll be able to mess with our cost and distances a little bit better with floats later on so we're going to say this is we have we have a cost right and we had well first thing too is we need to add something else so we're going to need an aggro range right for our enemy so let's add that in we also had an attack range we had attack we had health and we had armor okay now in order to see these um in our and other uh to serialize these in our other classes um let's actually put a um system dot serializable okay so then we'll save this and now this is going to also allow us to create multiple different type of stat templates for our unit templates so later on we could come down here and say upgrades or you know whatever else we need to add later on so let's go ahead and fix this now in our basic unit so the first thing we need to do is just get rid of all of this and we're going to say public unit step types dot base okay and we're going to call this base stats okay we need to actually fix this in our unit handler as well otherwise we're going to get compile errors and we're not going to get to see these changes right so we're accessing our unit but now we're not just directly accessing the cost right we're accessing this cost okay so we're accessing its base stats so in our unit handler script just add base stats dot cost okay so what i'm going to do here is i'm just going to copy this up to our dot um here here here and here now we also added another thing here and we need to also make these floats so we're fixing this now so we don't forget later okay because we can't change a int between into and float because a float can have decimal points and an int cannot so after this one we had a float aggro range right okay so now in here we're gonna have to also add unit dot base stats dot aggro range okay i'm just going to split this up a little bit so you can see it better and then in here we just have to add another zero because we added one more step now that fixes this function so let's just come down here and we'll fix it here as well so in order to fix these ones the first thing that we have to do is we have to go back to our player unit right because we're not going to be setting these anymore and now we have an easier way of doing this so we're going to do the same thing that we did in our basic units but we're going to create a new one right so public unit stat types dot base okay and then we're going to call this base stats so now let's save that and head back over into our unit handler and since we already copy pasted the dot base stats there i'm just going to do it here as well so we can say base stats and i'm actually going to show you trick here now too so if i hold alt and shift and then i just drag my mouse where i want to go it highlights each one of these lines and i can press ctrl v to paste and it pastes them all now i also need to add in another one so we're going to say pu base stats and we have aggro range right so equals stats dot aggro range now because we have all of this set up all i have to do is copy this and we're going to put this in for our enemy stats but instead of pu you notice we have we have to access our enemy unit script so we're just going to change this to eu okay and we're going to say eu and we have to reference it here too so we can actually just cut this out because there's no need for it to be there right so we'll just put it here okay so playerunit pu okay and then i'm just gonna paste it here as well because it's easy to edit and instead of player unit right we need our enemy unit script now so we don't access player we just access our uh enemy so enemy dot enemy unit okay so that's eu equals unit and we don't want to grab the player unit either anymore so we'll do the same thing here enemy dot enemy unit okay so now we can change all these i'm do this the same way before you hold alt and you hold shift click and drag straight down and um actually i don't want to do it right there i'm going to do it right here okay so now i have all of these p's and i'm just going to change them to ease okay so now we have eu.bass stats okay so it's saying we don't have this yet and that is correct so let's save this and let's head back to our enemy units so we can start working on them and we're going to do the same thing that we did in our player units and we're just going to say unit stat types dot base base stats okay and over here we're going to say that this is public okay so now if you look we have everything uh wrong one here we go we have everything fixed in here now so let's actually go look at what that does before we start finishing up our um enemy ai so now when we click on these you actually see we have this little drop down and now we can see our cost our aggro range and all of this inside of the drop down so there we go and you know what before we forget let's go on ahead and set these so i'm just going to say the cost is 250 we'll put the aggro range let's say i don't know um 10 or yeah 10. attack range can be uh 10 as well attack they do 25 attack and have 50 health okay warrior we're gonna do essentially the same thing we're going to cost 100 aggro range is going to be 10 attack range is going to be one how much attack do we want for these guys let's say they do 50 damage okay health um we'll give them a hundred armor suppose we could give them a few right let's give them five then a worker will do 25 our aggro range let's just put up two this we're just going to leave them all as 10 right now attack range 1 attack let's do 2 and health of 50. well let's do 25 there now that we've done that let's go ahead and change our infected unit prefab for now to our enemy units layer and yes we want to change the children so this way we can access our enemy units based on the layer so we want to go into our unit handler script and create a reference for this so in order to do that we're just going to add a public layer mask okay it's going to be p unit layer and we'll have e unit layer okay then in our start function because this is going to be after the instance so no matter what we can still reach it okay we're going to do our e unit layer equals layer mask dot name two layer and the name of the layer is enemy uh actually i think it was enemy units right just to double check let's see yep enemy unit so we have enemy units and player units so we'll make the player unit one p unit layer equals layer mask dot name to layer and we'll do player units perfect now that we have a reference to our layers let's hop over into our enemy unit script and get to work so the first thing we're going to need a couple variables here we need a way to detect our if anyone has entered a certain radius and the way that i want to do this um is going to be with colliders so we'll do a um a collider array and we'll just call this um i don't know range colliders okay and then we'll also do a we want a transform right so we can set the target so we'll do our aggro target then we will check if we even have aggro because if we already have aggro there's no need to keep setting it so has aggro and we'll just default this to false okay um we also want to be able to check the distance between us and the person we're chasing otherwise we'll never know um if we need to stop or not right because um we want to keep running if our if our distance isn't where our point of destination is right so let's do a or if our distance is greater than our point of destination so private let's make this a float distance okay so now that we have our variables um let's first add a way to check if our targets are within our range so let's write a private void let's call this check for enemy targets okay inside we're going to set um oops our colliders and we're going to make these they're going to we're basically going to set a a sphere distance around our character to check if anything enters this area so we're going to do a uh we need to access physics so physics dot overlap sphere um and we want this to be placed on our on this unit's transform position and the size of it we're going to use our aggro range so we want to access our base stats dot aggro range okay now we need to check if what entered this was actually an enemy target and not maybe just you know a house or a bird or something like that so we'll say for int i is equal to zero then i less than a collider range collider oh i was wondering why this was spelt all weird okay range colliders there we go okay um i is less than range colliders.length then i plus plus so we're going to check starting at the start of our collider length we're going to check each one of them and we're going to the first thing we want to know is if um the range collider that we're currently on which is why we're going to use the i if it's game object layer so if the object that we're checking if it's currently on our unit handlers um we need to check instance and then player unit layer oh we need to say is equal to okay so if it is if it's on our player unit layer that means it's one of our player units right so in the case that it is then we're going to set our aggro target equal to um our collider our range colliders um i game object dot transform okay so we're gonna we're gonna check um if the object that entered is our layer and if it is we're gonna set that as our aggro target and then we need to say that our has aggro is equal to true because now we do have aggro and then we want to break so this means that we're not going to run this for loop anymore so as soon as something enters in here that is a player unit he's going to grab aggro and he's not going to keep checking anymore because there's no need to keep checking okay so now we need to set a way for him to actually go after our player so let's do private void and we'll say um well let's not say attack because this isn't actually attacking yet right so um we'll just say move to target move to aggro target okay and inside of here we want to set our distance equal to a vector 3 and so we're checking the distance right now between two different points okay so we're going to do vector three dot distance and the points that we want to check are our aggro targets position and our position okay now um we want to have our nav mesh agent we're going to set our stopping distance which is going to mean we don't necessarily have to get directly to the point we said because if if this is our player on you know this point if we set our destination to this point he's going to want to run all the way into the point so we're going to set a stopping distance so he can stop here and assume he's at the point okay so we need to access our nav mesh agent which we have not set yet so let's come down here and we'll do a private void start and we need to set a nav mesh so let's get a reference to our nav mesh agent so we'll say nav mesh agent um nav agent okay and let's set this to private okay and then in our start function we'll just say nav agent is equal to gameobject.getcomponent nav mesh agent okay now that we have access to our navmesh agent then we want to put our nav agent dot stopping distance okay and this is going to be equal to our base stats dot attack range right because if we don't have to run all the way up to them then there's no point in running all the way up to them if i could shoot you with a gun i don't need to stand point blank to do it right so let's say base stats dot attack range and i'm gonna do plus uh plus one here uh because we had some of our units set with only a stop um an attack range of one which it doesn't give them enough space for our units right now and this will actually be adjusted later on depending on what our actual models look like right so that there's our um there's our stopping distance um now let's do we need to check if our distance is less than or equal to our base stats aggro range so we're going to check if we're within range and if we aren't there yet then we'll put our nav agent set destin oops set destination um and we're going to set that to our aggro target.position there okay so now we need a way to call these functions so let's just uh for now we're just going to put this in um just straight into our update okay um and the way that we're going to do this is we're going to check if we have aggro or not okay and we don't currently have a way to set our row to false and because we're not actually killing a unit yet we're not necessarily ever going to get out of range but this is going to at least make our aggro work so we'll say if we oops if we do not have aggro then we're going to check for enemy targets okay if we do have aggro then we're going to move to our aggro target all right now let's go ahead and hop back into unity and give it a whirl uh first actually though we need to make sure we put our enemy unit script onto we're just gonna slap it on infected unit for now just because they're red and they're what we're testing with so let's hop down here we can look at workers infected unit let's just make sure it added that navmesh agent which it did if it didn't then you actually want to remove this object and then place a new one in there or you might have placed it on there too soon and you need to just remove the script and re-add the script okay so now when we hit play and it looks like we were already in the range right so um let's actually move this out a bit and we're going to rearrange our units so that way we can really see this happening we're going to set our units here on the right side of the screen we'll put the enemies on the left side okay now when we hit play you'll notice that if i grab this unit and i run into his range he's going to start chasing me and i can run away and he'll just keep following now the other good thing about this is because we make it stop after it gets a target we could grab all of these guys and run in here but the first one that he sees that's his target right so now if i was to move him he's still gonna chase him right he can't he's not just gonna switch targets he already has his aggro target and there you have it guys we can actually run away from enemies that are chasing us our enemies will aggro whenever we get in their range and they'll stop within a certain distance for us to adjust for their attack range and we've also set up a way to make our unit stats a lot cleaner and easier to build on in the future so i really hope you guys enjoyed this one and until next time [Music] peace you
Info
Channel: Lithex Productions
Views: 2,840
Rating: undefined out of 5
Keywords: lithex productions, lithex, productions, game, game design, pc game, mmo, rpg, game development, game designer, how to make games, indie game, game code, unity, unity3d, c#, c# for unity, rts, realtime strategy, strategy, indie dev, howto, tutorial, how to
Id: l0GhE9N0o9c
Channel Id: undefined
Length: 29min 26sec (1766 seconds)
Published: Sun Apr 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.