Part 34 - Patrolling Enemies: Make a game like Zelda using Unity and C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Really appreciate these, thanks!

👍︎︎ 6 👤︎︎ u/VulvaSpud 📅︎︎ Dec 18 2018 🗫︎ replies

Thank god i found this tutorial

👍︎︎ 5 👤︎︎ u/Zer0men 📅︎︎ Dec 18 2018 🗫︎ replies
Captions
hey there welcome back so today we're gonna be making a patrolling at me that came patrol between I just have it set up as two points but I discuss how you can make it be more than two points you can make a nice little circuit here and we're not losing any of that inherent behavior in this character though for example if I switch over to the game view here my player who's going to get within range and then be chased but if I leave the attack or the chase range then the logs going to go back to following its little paths so there we go um let's dive right in and get started [Music] okay so let's dive right in let's talk about how we're going to get this all set up so the main idea is that we want to have an enemy who patrols between a certain location but still when the player gets near they'll still chase the player and then when the player goes out of their range rather than going to sleep like this enemy does we want them to go back to whatever their last destination was and then go back to patrolling so I don't have another sprite setup for this I'm just going to use another log so what I'm gonna do here first is go to the log that we have already in our scene I'm gonna make sure I have this as a prefab I have log in my prefabs but I think yeah this is a super old one so I'm gonna delete this one yup and I'm gonna riad my log to my prefabs so that now it has the knock-back script and the log script and all that good stuff so what I need to do now is I'm going to take another one out of my prefab folder just place it into my scene and with this highlighted I don't want this to be part of that log prefab many marks I'm making a new kind of enemy so with my my new one highlighted this one right here I'm going to go up to game object and break prefab instance so this is no longer order that prefab I'm gonna rename this from log to patrol log and then I'm gonna pull this into my prefab folder so now this is its own prefab now what I want to do for my patrol log here is I want to create two points in the world so to do this so that my my hierarchy is already getting kind of cluttered so to keep things kind of clean I'm gonna create an empty game object and I'm gonna put this at zero zero zero and I'm gonna call this game object patrol points and then inside here I'm gonna create two new empty game objects I'm gonna give them an icon so I can see them in the world and be able to move them around so I'm gonna pull this over here and I'm gonna call this point one and then I'm gonna duplicate that and I'm gonna call the duplication point two and I'm gonna move that over here so these are my patrol points now it was important that I put this main parent object back to zero zero zero because if I hadn't then these would have positions that aren't the same in world space as they are relative to their parent so like right now these positions are relative to the parent object and if the parent object weren't at zero zero like say if the parent object is over here this one could be zero on Act's even though it's not really zero on X so when I tell the log to aim itself towards this it wouldn't really be aiming itself towards that location it would be aiming itself towards the world space coordinates so I needed to kind of recenter this main one here all right I'm gonna do a few other things to clean up my hierarchy here I'm gonna create a new empty game object you can't create folders in your hierarchy so this is the the general practices instead is to create game objects to act as holders for everything else in the world so I'm gonna call this game object enemies and again I reset at position two zero zero zero so that it doesn't mess with anything and I'm gonna grab all of my log enemies here all three of them that I currently have in the scene and pull them into my my enemies folder and just make sure I got the right ones oh no for some reason I called this guy patrol log what the heck this guy's patrol log oops all right so trol log this one is going to well I'm gonna delete my patrol log from here delete and then this one right there you just gonna delete that from the scene and leave just the two this one that chases within a certain radius so I'll call this I don't know maybe like range log and and I still have this one here that's just a regular range log as well okay cool we're good alright I'm gonna create another game object here what's this for oh that's that dot I was using to size my room so I can delete that now I'm going to create another game object I'm gonna call this after I reset it to zero I'm gonna call this one objects for like the treasure chest and the pot and assign the event system can stay there okay that's a bit cleaner alright so now for my patrol log here I'm going to remove the log script and instead I'm gonna go to my scripts and let's organize this a bit more - let's create a new folder I'm gonna call this enemy scripts and then this is gonna get the enemy base class the log and not the signal listener so then inside here I'm gonna create a new C sharp script and I'm gonna call this patrol log all right and you know it's saying that it can't find the script I think it's actually okay then I move that that yeah it's fine okay so I'm gonna open up my patrol log script here and I don't already have Visual Studio open so it's gonna take a minute so I will meet you back here okay so I want to inherit a bunch of the behavior from the regular log script and I want that behavior for free for example I want its ability to chase the player so what I want to do is have this inherit from the log instead of inherit from enemy because if it hurts from log and log inherits from enemy it's getting all of the enemies stuff but then it's also getting all of the log stuff so by default this will chase our player here I also want to in my log script there's all this stuff that I'm doing in my start method I'm setting the enemy state I'm setting the rigidbody the animator and the target and I want all of that to stay so I'm just gonna get rid of my start method here and by default that should use the base classes start method but I'm not 100% sure on that let's actually make sure here so I'm gonna save my script I'm going back into unity here and as soon as that compiles I'm gonna add my patrol log script to my patrolling log you move my animation palette out of the way here and I'm gonna look at my scene I'm gonna hit play and it's gonna jump over to the game view right away but I'm gonna switch it back to the scene view so I can see this guy so I've got a no reference exception object not set enemy dot awake from patrol dog and that's from enemy line 23 so let's see what that is oh okay it doesn't I didn't set any health for this guy all right so in my patrol log needs to have max health so for my scriptable objects my enemy Health's I'll give this guy a to hit enemy health and I'll give him a name I'll call this patrol log I'll put his base attack at 1 this move speed it - I don't have to fill in target is chase radius is gonna be 3 it's gonna be relatively small his attack radius is 1 and then I don't worry need to worry about the home position or that animator so let's try this again let's hit play and there we go I flip over to the scene view and you can see that already it knows what the target is it knows what its animator is it knows all of that information before we even start anything so the one thing I want to do really quickly here is I want this to start awake so what I'm gonna do here to add to this in my log script yeah so I want to set that wake up to be true so if I set this in the base class it should be fine so Anam dot set bool I call it it's a capital W yep no lowercase W I'm gonna set wake up to be true and then it should set itself to be false on that first fixed update because it's gonna be I'm just I'm worried about the the other the other logs I don't want to mess too much with their base script because they were working at least okay so I'm gonna hit play here okay cool so that didn't mess anything now the main thing I want to do is I want to change this check distance method so if we're within distance of the player that should be the main priority we want to chase the player if we're not within distance of the player rather than just setting that wake rule to be false and having the law go back to sleep I wanted to do something else so I'm gonna grab all of this check distance I'm gonna copy that I'm going to go into my patrol log and I'm gonna add that as a method here so check distance now I'm not going to need to use the update method and that change animation in order to call that from here I need to make that public so I'm gonna make this a public method and then I go back to Patrol log my rigid body it should know what my rigid body is oh I've got it private so I'm gonna make that public as well so this is public my rigid body and then change state so we don't need to change state because this enemy is always going to be in the walk state it's it's never gonna change its state all right and then rather than having this wake up you get rid of that now in order to do this what I'm going to do is I'm going to make this check distance method here this is gonna be a public virtual void and that means that I'm going to be overriding it from a class that inherits from it so this is a virtual void meaning that unless it's overridden this is what we're going to do but if it's overridden by an override void which is what we're going to do in here in the patrol log script this is a public override void that is going to override the base class so this inherits information from the log class but we're gonna override the check distance when the check distance is called from the base classes fixedupdate which means it's going to be called from this class's fixedupdate all right cool now what I want to do here really quickly is I want to add some information about what its path is so I'm going to create a public transform array an array just means there's more than one of them but I'm going to call path I'm also going to have a public int which is going to be the current point and then I'm gonna create another public transform that I'm gonna call current goal how about so I want all that information there now back into unity here I've already made those two dots that I want my my little Deuter here to patrol between and that is in my Patrol points object so I'm gonna lock my screen or my inspector so that I'm looking at the patrol log I'm gonna go down I'm gonna find the path and right now the size is zero so I'm going to open up the patrol points I'll highlight both of them and I'm gonna pull those in to that so that now I have point one and point two now by default I want my current point to be zero so in unity that's going to be my current point I also want my goal to be just the first one here then once it's reached shores within a certain distance of this first one I want it to start moving to the next one and then if the next one is the last one in this array I want it to reset to the zero one now what that means is if you have like an l-shape here from here to here to here if I were to reset back to zero it would make this cut across so if you want it to patrol in an l-shape you need to make sure that you're doing 0.1 0.2 0.3 and then point two and then it should go back to the last one so kind of keep that in mind when you're creating your path that you want you're patrolling enemy to follow okay so now back here in Visual Studio I'm going to create a little method here that I'm going to call change goal so this is going to be a private void change goal and what this is going to do is it's going to check to see what the current goal is so we're gonna say if current goal is equal to path length minus one so if the current goal actually yeah so if the current goal is zero and there's like five points here mm-hmm path length should be fine oh it's not current goal it's some current point because I want to do the the integer value so if we're currently on point one and there's two points in our thing that means that the length of this is 2 but 2 minus 1 is 1 so 4 on point 1 which is the end of the path then we want to reset it we want to say current point is equal to 0 and also current goal is equal to path 0 so we're resetting our current goal to path 0 we're gonna say else we're going to increase our current point and then set that current goal equal to path current point so whatever point we're currently on okay now this is something I want to do from sorry I lost my train of thought this is something I want to do every time that we reach our our destination so if we're not chasing the player which is what all of this is about then what we want to do is we want to move towards whatever our current and goal is so I'm going to steal a little bit of this code here I don't need to do my I don't know I'll still change animation so I'll copy this and I'll paste it in here and this doesn't have a temporary vector so my temporary vector I'm gonna steal as well so you grab that so copy and paste so vector 3 temp move towards but I'm not going to be going towards target position I'm gonna be going towards path current point so I'm going to be moving towards whatever the current point in the path is and I want to move towards its position because if it's just current point that's just the transform I need an actual position and then we're gonna move there you do okay and now I need to have something that's gonna check to see how close we are and because we're dealing with floating point values we could be very very close so I don't want to check to see if the position is exactly the same as the point instead I'm gonna bake in a little bit of ease so I'm gonna make a public float and we'll call this I don't know rounding distance so I'll set that to something small like say 0.2 or something like that and then in here I want to say if vector 3 dot distance between our target or not target position between our transform dot position and path current point that position let me make some room here so everybody can see what I'm doing Visual Studio does not like what I'm doing right now that should be a little bit better so if our distance between our our transform position and our path current point position so where we're aiming ourselves to is greater than rounding distance then we're gonna do all of this and if it's not we're going to change our goal alright I feel like I'm doing a really bad job explaining how this one works so we're taking the check distance that was already made for the log and then rather than having the log go to sleep if our distance from the target is greater than the chase radius and we're going to check to see how far away from our goal is and if we're farther away from our goal than the rounding distance we're gonna move towards our goal if we're not farther away from our goal in the rounding distance then we're going to change our goal and to change the goal we're going to take whatever our current point is we're going to check to see what our current point is and if our current point is equal to one less than the length because we're starting at zero then our current point is going to go back to zero and our current goal is going to be equal to the first index in the array otherwise we're gonna increase our current point and then reset our goal to be that part of the array no one more thing you need to do here so our current point our current goal is going to be oh wait I need to do past current point that yeah that's right I could also just do this as current goal position that would be simpler I don't know why I didn't do that sometimes I'm stupid um alright so now the problem here I'm gonna save this pop back in the unities that you can see what the issue is going to be I'm gonna get a null reference exception error right away and as soon as it's done compiling I promise you I'll get one nope I didn't get one oh okay cool so there is he's already walking back and forth why didn't I get a no offense exception error on that with the current goal that's weird okay well I guess I'm wrong so here he is just bouncing back and forth his rounding distance is zero it really should be something like zero point one because a floating point on which you can't really see any difference just watching it now if I flip back to my game view and take my player let's make sure that the log is still inherited the chase behavior and they have and if I go away see how he makes his way back up to his little path there Jace sorry for that weird cut there okay oh this Deuter doesn't have a knock back so we need to give him that no he does he does he has the thrust of to knock time and a damage he doesn't know he does have the enemy take okay so I'm gonna go out of my lock here and there we go we've got a nice little patrolling enemy that's also gonna chase our player so if you have any questions please feel free to ask them in the description down below hang tight for a quick announcement at the end of this video and yeah I hope everybody out there has themselves a wonderful day hey this is me from the future there's little issue at the end of the video where if you are running into the log the players not being knocked back which is a little weird I thought about it for more than about five seconds and I realized what the issue was so the problem is that the player needs to be so if you go to your player you rigidbody2d you need to change your sleep mode to never sleep and I'll explain what that does here so if I jump over to the scene if I'm looking at my player right now my box Collider you see how it's kind of like grayed out a little bit that's because the players rigid body is asleep I'm gonna change that to never sleep and you see how it kinda lit up like that so now our player is awake which means the rigid body is receiving collision data like they're supposed to so there we go all right if you enjoyed this video consider giving a like subscribing to the channel or telling a friend who might be interested also please consider following me on patreon for as little as a dollar a month you can earn access to tangible rewards like early access to videos backer only videos in series holes for future topics streams and even individual tutoring sessions you can find a link to that in the description and as always have yourselves a wonderful day
Info
Channel: Mister Taft Creates
Views: 13,391
Rating: undefined out of 5
Keywords: Unity, Unity Tutorial, C#, C# tutorial, Unity Zelda, Zelda, Action RPG, Unity Action RPG, Unity Zelda Tutorial, Zelda Tutorial, Action RPG Tutorial
Id: qxAwkzvwOOQ
Channel Id: undefined
Length: 26min 9sec (1569 seconds)
Published: Mon Dec 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.