Enemy AGRO AI System in Unity For Beginners ( 2D Game Dev Tutorial )

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Great Tutorial! The people who make these kind of videos are the true heroes ;)

👍︎︎ 1 👤︎︎ u/PauEretsu 📅︎︎ Dec 07 2019 🗫︎ replies
Captions
hello guys John stairs girl here welcome back to it another game dirt video so in this one and we can look at how to implement a simple but effective enemy aggro system where the player walks up to the enemy the enemy wakes up and chases the player around the screen it's gonna be a lot of fun and really easy to surf for beginners I'd also like to quickly thank those of you who are supporting me on patreon you guys are absolute heroes and I couldn't do this without you so thank you very much for your support so let's get straight into this tutorial [Music] so to save us a bit of time I've created this scene here with some basic game components I've got a player I've got an enemy and just the ground and a camera and a background just to save us creating a player control from scratch now this play controller here is the same one that I created in the previous Megaman tutorial so if you want to learn how to make a player move and jump you might want to go there so in this tutorial I want to walk up to this enemy object and you can see currently he does nothing and I want him to kind of wake up and start chasing me and has some kind of an aggro range maybe here so that when I enter it he wakes up and chases and when I leave it he kind of goes back to this non aggro sleep state so that's pretty cool so by the way I hope you liked these characters I created these specifically for this tutorial I'm going to be using these a lot more because I think it's a really simple way to understand the concepts without getting caught up with complex animations and run cycles and stuff often they come with their own complexity so the first thing you'll want to do is click on the enemy that we want to use and you'll see here that I've got a box Collider 2d you can see he can see the shape here and I've got a rigidbody component now you don't have to use a rigidbody this particular example and many of the examples I use I create using rigid body physics it's not the only way it's not the only approach you can do it with non rigid body physics some people hate the rigid body physics some people don't but the reality is most beginners coming into unity for the first time are going to be using the rigid body physics system it also allows for like really easy cool effects like pushing crates around and these kind of physics stuff so up to you but anyway that's what we're going to do so what we'll do now is we want to create a new script for this enemy and this is going to hold all the coded that tracks to player so go click add new let's say new script and I'll just call it um enemy which we call it enemy aggro well you know what just call it enemy script so it does double-click that script here now and will open any Visual Studio will want to start by declaring a few properties at the top of this enemy script class so one will need is a game object well actually it's mega the transform transform called player because we'll want to keep track of the player it's a very important one and we'll serialize this field which means we want to expose it in the inspector panel for the enemy and what else do we want to do we might create will create a float and we'll call it a grow range and we might serialize this one too and we'll create another float I'll call this one move speed also serialized is filled and finally we just need a reference to the rigidbody which we're going to be using it's to add force to make the actual movements so you don't have to serialize this one are B to D is the kind of naming convention I like to use for rigid bodies RB rigidbody 2d it is very simple okay so in the start function will first while we here we will assign this rigid body here so let's say rigidbody 2d equals get component so we getting the come getting the component on this enemy rigidbody 2d right so I might just jump back to unity now so with the enemies selected here you can see that only the enemy script those fields we see realized are now available here so what we'll want to start by assigning the player so two ways to do this you can click this little circle here and find the player in the scene tab here or you can click the player in the scene view hierarchy just drag it all the way across in I kind of like that I don't know I kind of like this whole dragging business sometimes when you click on this file it can be quite a few things I mean you can search and stuff but you know so aggro range we all set this to four so what this will represent the aggro range is will be the range here how many units of measurement before he sees the player and movespeed will start with two and we're going to likely adjust this as we go but for now let's get this guy moving the very first thing we'll doing the update function is I'm just going to put a comment here for you guys you want to check the distance to play out and it's really cool way to do this so we'll create a local variable within inside this function here I'll just say dist to player equals vector 2 dot distance you can see here the order the context menu is showing us returns the distance between a and B this is very powerful it's very quick quick way to get the distance between two different positions so we'll start by saying that transform position which is the enemy's transformed a position so I'm referencing the enemy's position and the second is the player which we declared up top dot position right so what we can do just now quickly we can just do a print or debug dialog however you like to do it we might just log this out just to show you guys what's actually happening now so this should we jump back to unity and hit a run this should show us in the console our distance through that so distance is 911 and as we get closer so we can actually this is actually a very effective way to work out the range we want visually so we can just kind of test this out and how close should we get maybe around this close right maybe you should wake up around here maybe five so might just change that Edgar arranged five so anything anything of less than five we want to aggro the enemy now that we have the distance to the player I'll just get rid of this now because we don't need that so I'll say if the distance to player is less than the aggro range then code to chase players block this in for now and if it's not smooth this down a bit then here we want the stop chasing player so start here and I might actually making we could direct directly in here but I might make a new function called it chase player just to keep it clean and it's kind of a nice way to do things because otherwise this whole block of code can get very messy so I'll say stop it stop chasing player and I'm keeping these function names quite long and I'm doing this on purpose so you guys can understand exactly what these functions are doing so you need a quote we need to create a new function so very quickly for you guys who don't know you can hover over this name and you can see this little drop down here if you click that little arrow and click this button here it has automatically created the function for us how cool is that alternatively you can write it private void stop chasing player you could do it like this and hope you got this spelling right and by the way these don't have to be marked as private they'll autocomplete it kind of did that this is probably okay just a door like that that's essentially the same thing it also generates this kind of rubbish stuff that we don't need so we'll say if the transform of this enemy of position dot X is less than the player dot position dot X well that means what does that mean that means we are on the left of the player and we want to move right so let's say rigidbody2d dot velocity equals new vector2 and he will say move speed which we declared early on that's for the x and y we'll just keep it as zero because we're not moving vertically and alternatively we'll write an else and this means the else would represent if we are to the right of the player if that makes sense to you guys and I'll explain that a little bit more in just a moment because I think that could catch a few people that aren't quite following on as well so we'll just say here enemy is to the left side of the player sort of move right enemies to the right side of the player so move left technically I should have done this follow me else if transformed the position equals this now what this means is if there is a condition situation here that we've got two conditions here one is checking if we are to the left of the player the other is checker checking if the enemy's to the right of the player but what if the enemy in the player had the exact same x position in that let's say that the player is standing on the enemy's head exactly in the right spot well nothing would happen the player would not sorry the enemy would not chase the player which is why I just did this else if we are to the left of the player run right if we are to the right of the player or on top of the player directly there the left and you can see here I've just made the move speed and negative so let's run that I hope that made sense to you guys um I'm trying to explain it for a beginner's mind set some of you might be intermediate so you kind of like I get it I get it stop going on about it so let's see what happens well look at that look at that he's coming he's coming whoa he's not stopping he's not stopping whoa go away from me holy crap this guy's like the sleet is sleeping zombie get away this is pretty cool but I like this there's something here it's kind of the simplicity of it is kind of really cool go away get out of here push him yeah this is fun is this strange really just keeps coming with his eyes closed which makes it really funny so okay so you might realize now we are not we have we need to implement the code for the stop chasing player so very simply we will just say rigidbody2d dot velocity equals vector to dot we can say to zero and what this is it's a it's a shortcut to writing well you know what I'm gonna write it a long way just for you guys because new vector2 zero zero right that makes sense okay so we're just stopping all the movement here we're setting movement like move right move left and here we're just stopping it just for you guys they want to make a big deal about me using rigid bodies and physics in this code here you're welcome to move the enemy however the way you want you can use translate which is a functionality of the transform not trans yeah translate that also moves the enemy you can times the exposition of the transform by Delta time to move it it's purely up to you I'm just using the velocity as a example here well Yesi so when we run away and of the aggro range he stops you might notice one thing that the enemy is not changing his he's not facing the player when he chases the player so what we want to do here so if we are right of the enemy we want a facelift so we'll say transform dot local scale it was a new vector2 and we'll say negative one comma one so we're saying we want the whole enemy object to turn around to a scale of minus one so that will flip the whole object around so we just copy that and go up here and contrary if we are on the other side of the player if we on the left side we want to turn back right so we'll get rid of the minus so here we turn right here we turn left the other thing you can do you can do things like to make an example get a component sprite renderer dot dot flip x equals truth flip x equals false but all that does is it just changes the graphic it doesn't actually change the whole object if you do this everything if you have a nested child if you have block all the box colliders and everything everything flips around so this is a much better way to do it oh yeah it's working say we on the left he turns left we jump to the right he turns to the right so this is looking really good I'm really enjoying this and the final piece of this puzzle is to make this guy kind of wake up and I think we're just gonna you don't have to do this but I think it'll be fun for this example just to visually see it so I'll just quickly so if I click just give you overview on what I've got here I've got an enemy face here you can see it and in that enemy face I've got a bunch of animations I've got eyes closed and eyes angry so I want to switch to this one so face eyes angry this the name of the animation and face eyes closed so quickly show you how to do that this is probably less relevant for your own game because you probably you know have your own setup but by the way this whole project I've got a bunch of patreon who have been very very helpful and generous in supporting this channel and one of the incentives I like to do for my patreon supporters is to give all my full project files and source code including the assets to them I have a folder that I make available to you guys and that's want to thank all you guys here who are currently supporting me there's only a few of you so I want to really thank you for being pioneers in this space and really helping this channel to grow I won't forget it and I already appreciate it and those project files will be coming your way straight after this video goes live so ok so what was I doing I want to make a reference to the face component so Gabe object face you know and I'll just see realize that and I just want a reference also to the and and him animate man look how many different things to write here it's easy to get confused animator yes of course and I'll just call this face animator in most cases you'll likely have an animator for the whole game object but in this case it's the face because this cube is are really changing and it'll be a little bit redundant to have a entire sprite sheet just for blinking eyes so you can see here I'm just to make be clear I've just got a face so we need to assign the face I'm just going to click that face and drag it into that boom and we'll say face animator equals face which we just dragged in dot getcomponent animator so we are getting the animated component from the face game object all right so now all we have to do is that within this chase function just now here below it will say face animator dot play and all we now do is put in the name of the animation and that was face eyes I think that was it so it's a bit of a long name but you know I guess see in a small project like this you know it doesn't make much sense but if you had like all these different types of enemies and all these different types of faces and body parts it's a very efficient way of naming it it's the face component it's the eyes and the state is open so you know that was my logic if you're wondering why I made it actually I think that was angry face eyes angry and what I'm going to quickly do I felt that he was moving a bit slow so I'm gonna bump up that move speed to about three might change the aggro to about six just to make it feel a bit more frightening so let's click play why did not open his eyes what did I do wrong face animator to play faces closed face eyes open let's check those animation names again oh I use the capital F I'm sorry how foolish of me I condemn you to hell for your transgressions alright this is it guys this is it oh yeah and at that point we'd probably kill the player get off him get away get away quickly get away Oh are we safe all right guys we're gonna creep up we gotta get behind him to get the magic sword loot the treasure chest save the princess oh this is fun I could play with this not all day but for at least ten more seconds this is really cool I'm really digging this so that's all well and good but check this out this situation proposes a problem so I've got this crate prefab where is it okay so I've got a crate prefab so what will happen if I put this crate in between see the problem is the enemy can still see me you see because we're doing our check is purely based on distance which depending on the game you're making this may be okay it's maybe what you want no not I mean not every game has to be where the put way the enemy can't see through objects but it does propose a problem and what I want to do in the next video I'm gonna show you guys a more advanced way of doing this Agro check and what I'm gonna do is I'm gonna shoot a ray out of the enemy's eyes essentially and look for the player and if that array hits another object that isn't the player then he won't be able to see the player so effectively gonna create an accurate line of sight from the player sorry from the enemy to the player and that will be a really good place to go with this but for most for a lot of use cases for simple kind of enemy aggro what we've done here is more than suitable because the reality is depending on the game you're making you may not even have these kind of situations with crates maybe a game doesn't have crates you know it's not really a big deal let me know what you guys think if you've enjoyed this video please do give it a like and drop a comment down below and if you need any help at all come swing by the discord I'll put the link down below and you can speak to me personally there or I've got a it's like 150 people there right now and a lot of very talented people who are helping people out and it's really easygoing place and you can come there to get some support all right guys all the best don't forget to sub to the channel if you're new and I will see you in the next video see you guys you
Info
Channel: Lost Relic Games
Views: 35,536
Rating: undefined out of 5
Keywords: enemy agro, make enemy move towards player unity, simple enemy ai, make enemy chase player in unity, game dev classroom, agro, enemy, unity 2d tutorial, ai, agro range, tutorial, enemy find player, enemy search for player, enemy follow player, enemy move, enemy controller, enemy chase player
Id: nEYA3hzZHJ0
Channel Id: undefined
Length: 23min 27sec (1407 seconds)
Published: Fri Dec 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.