Godot 3 - Platformer Tutorial - Part 8 - We Need Enemies!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everybody welcome to a my pixel as always it's awesome to have you here today we're gonna create some enemies and get them moving all over the screen because after all what good are fireballs without something to shoot in mad right you ready okay let's do it okay so the first thing we're going to do is we're going to create our enemies scene right we don't have one yet so let's just click up here create a new scene first thing we're gonna do is similar to the player we're gonna put a kinematic body 2d in there okay and we will just name that kinematic body 2d enemy hey the enemy also needs an animated sprite we also need a collision shape to dok2 the collision shape to be will add the collision or the rectangle after we add the animated sprite so first let's go to the animated sprite we're gonna add a new sprite frames go into the sprite frames this default animation will just uh we'll just make him walk okay standard for our project we're going with a twelve FPS animation something a loop to on because we're walking alright we want that to loop over and over again hey now we need to bring in our sprite frames let's go with I think it's the enemy oh one okay so we're going walk zero one two and three okay and that's our walk animation then we need to set our collision shape let's zoom in here actually let's make sure we save our scene here we're just gonna save it as enemy that's fine okay then we get into our collision shape we will add a new rectangle shape and then let's as best as we can get to fit someplace around the the healthier that makes sense right see how it doesn't line up over here typically it's easier to make these things in some multiple of two as far as the dimensions right height and width for these they're not so I mean it's not a problem you just can't have publishing boxes that fit exactly unless it's a multiple of two okay so oh shoot we don't want to do that all right we don't want to use the outside angles we only want to use the inside handles and I accidentally use the outside handles there so let's not do that let's do it the right way all right well we'll get there folks all right let's use a collision box something something like this right this about makes sense okay so that'll be good for his collision box okay select this so that we don't actually Danny pull the collision box off there player anymore okay and then we'll save that so with that visually we have our enemy set off now we're going to go ahead and add a script to our enemy so to click on the enemy kinematic body here we'll just add a script all right you can select all the defaults that's fine and let's just get rid of this junk I don't know if we're gonna use the ready function yet so I'll just leave that in there over here we're gonna use the same thing like our player so we're gonna use the physics process to handle our movement and basically almost anything else we do to this elf all right well start off we'll give this elf some constant similar to the player right now just gonna have to obey gravity the same way the player does so let's given a constant for gravity okay then we'll give him a constant for his speed right just so that we don't have to modify that speed variable all over the place right we don't want to write 30 in ten different places and write 31 and we have to change them all so we'll just assign it to the constant speed then we're gonna have a constant for floor which if you remember is so that good ol knows which surface to detect as the floor so vector 2 0 comma negative 1 what else are we going to do here ok we're going to create a variable for velocity and we're just going to set that equal to an empty vector 2 or a vector 2 of if you don't put anything in there it's effectively 0 0 so it'll just be like that for now ok so then we're going to go into our physics process here if if this is the first view you all are seeing I know we're going through this a little bit fast but this is a part 8 in the series so we've already covered a lot of these concepts so I'm going through it pretty fast so if you want you know you can go and check out an earlier video in this series and we go ahead a little bit of a slower pace and you might be able to or it's a lot easier to pick up what we're doing right I go into more detail when we do it but we're just flying through it now because we've already done this similar type of thing to our player character in the past ok so inside of the physics process let's set our velocity X equal to speed okay and from the moment the enemy appears on the screen we want him to be in his walking animation so we'll just play the walk animation here okay our velocity dot Y right plus equals gravity this is just so that right the whole plus equals if you don't remember so that it acts exactly like gravity does the longer gravity pulls down the faster you move so that's why we do a plus equals okay let's see I like to have these things separated like this it just helps me to read it and then we're going to do a velocity equals move and the slide velocity floor okay and with that we'll save the script and the scene now we'll add an instance of the enemy to our stage one scene so let's jump back into our stage one scene okay so that we can see as you can see here the stage is a little bit bigger than we left it off last time what I did is I just went ahead and took the liberty of building out the stage bigger so that we'd have more space to put the elves so that we're not just working on this little bitty portion of screen so did that ahead of time but uh yeah that being said let's go ahead and put our elf in here so we'll select the stage one scene click this little chain link select the enemy and then we'll just drop the enemy in well why don't we drop them just right here that's fine he'll do fine right there okay so now he's in here let's run the game and see what we have so far okay so there are n amigos okay he's walked he's hitting a wall and he's not going anywhere else right so our enemy walks until he collides with a wall right but that's about right for what we've coded in so far so now we have to decide what do we want our enemy to do how do we want him to move around and react to the different types of environment today we'll have our enemy walk in the opposite direction when it hits a wall and also change direction when it hits the edge or hits the end of a Ledge this is a pretty classic behavior that you'll see in many games like Super Mario Brothers and Mega Man so let's get in there and code this behavior first we're going to have to create a variable to track the enemy's direction so we'll do that up here let's see we'll just say var Direction equals one now in this tutorial series all sprites and all initial direction that we use are facing to the right so we always say if the direction equals one then that means the direction is to the right of course you can have the number one represent the left direction if you want you just need to make sure to keep things consistent right you don't want one or the number one to represent right sometimes and then have it represent left at other times okay so we'll take this direction and then we're going to have to multiply it into the velocity X calculation so we're going to go down to our velocity X down here so instead of velocity x equals speed we're gonna say velocity x equals speed times direction okay so as you can see right positive speed would be to the right and negative speed would be to the left right so if we have a negative one and we multiply that in there then we get the character moving to the left so similar to the is on floor function then we don't use here but we use it in a player there is conveniently a function for is on a wall which I'm sure you can tell is used to detect when a kinematic body has collided with a wall so let's go ahead and make use of that so all the way down here after we make our character move we're going to say if is on you can see here we have is on ceiling wall floor this instance we're going to use is on a wall so if is on wall then Direction equals Direction times negative one so what this is is as soon as the enemy detects a wall right it's going to take negative one and multiply it into its current direction right which we already talked about it's gonna make it go in the opposite direction from whatever it's currently going so let's run our game and see if that worked okay all right well that's that's the start let's see what what do we also have to do we have to say if is on wall then we want him to also flip the sprite right so we need to do an animated sprite . actually sorry it is on the wall i think we should probably do that here so we'll say if the direction sorry let's let's try this again let's do it all the way by where we have the animations okay one more time let's see if we get it if direction is equal to one anyways fry play walk know that that's fine okay so if Direction is equal to two one then we're going to say animated sprite dot flip H equals false because he's already facing to the right else basically that's going to be a negative one or anything else but in this instance we're going to only going to have negative ones then animated sprite dot flip h equals two true okay and I'm always paranoid when I make changes right to see if they work so let's go ahead and do that let's see if that worked okay now he's banging to the wall he change direction he's actually colliding with the player also so then he change direction but that's working how we want it to right so now what are we gonna do we need to also detect how he's going to find a ledge right so let's go back into our stage one scene let's grab our player or another player or our enemy and let's stick them up here where there's a ledge and let's see let's just see what happens okay because we didn't cone any behavior for the legs right he only knows how to bang into walls he doesn't know what happens when he walks off a ledge he just keeps on walking gravity takes over and he falls to the ground so how are we gonna detect that ledge now I'm sure there are many ways you can go about doing this but today we're going to use a ray cast if you don't know what a ray cast is it's basically a line drawn in a given direction right you tell it which direction the line should go in if that line comes into contact with something then the ray cast will return a bunch of information that we can use to code the rest of our stuff right that that's the basics of it so we're going to go ahead and add a ray cast to our enemy so we get into our enemy over here we're gonna add raycast to D okay let's zoom out over here you can see this raycast it's just represented by an arrow the raycast actually has a one or like yeah it's a one pixel or a zero pixel thickness but it's just drawn really wide here so that you can see the only thing that matters is yeah the the center of where the raycast is right what coordinate it's sitting at so we're going to take the raycast okay and then let's see where a collision shape is we're going to want to move the raycast all the way to the front of our collision shape almost to the front one pixel back okay and then over here races see this raycast actually before we do that we need to enable the raycast if you don't enable the raycast it does absolutely nothing for you and for some reason the default when you add a ray cast is off I'm not sure why it's off because I would think if you add it you want to use it but and have it enabled but in any case make sure you select enable right okay and then there's this cast too so instead of 50 we just want to cast it right to his right to where his feet are all right so I don't know how many pixels this is is it 11 or something like that 11 it's not the bottom of the arrow where it's going it's right if you can see here right the 11 that we punched in is right here right at this uh the beginning of this arrow right where this line meets the triangle so we're gonna have to use a but let's use a 12 right so that it goes right to the feet that's all we're gonna need to check in fact you know what just just for good measure let's make that 13 okay so we will go ahead and we will save that so now our enemy has a ray cast added to him now with this our ray cast will collide with the ground until the enemy reaches a Ledge when he reaches the ledge the raycast will no longer be colliding when the ray cast is not colliding we know that we're beginning to walk off a ledge and that we want to change direction so let's go ahead and do the code for that okay so we're gonna put this after our is on wall function then we head in here take out some spaces so it's gonna do it do it's Walczyk right and then change the direction if we hit a wall and then we also want to check if the raycast is colliding or not so that we can detect a ledge so we'll say if raycast 2d that is colliding equals false right so if the raycast is not colliding then we know we hit we hit a ledge so at that point we're just going to do Direction equals direction times negative negative one so what we did the direction is the exact same thing that we do to it if it hits a wall looks pretty good now this is looking good and you might be tempted to stop here but we also need to move our ray cast our ray cast starts off by detecting ledges on the right side if you look at the enemy here and he's walking this way he detects a Ledge on the right side now if he walks to the left that ray cast is still going to be sitting on the right side and then he won't be able to properly detect the ledge so what we're gonna do is similar to what we did with the position 2d when we were shooting our fireballs so we're gonna add in that code so under if is on wall so if we change the direction we also need to change the position of our ray cast so we're gonna say ray cast 2d dot position dot X times where's equals x equals negative one so if the ray cast is sitting at let's just say whatever the coordinates are a positive six then it's going to go to a negative six which would put it exactly the same distance on the other side of the character right when he changes direction so we need to do that here when he bangs into a wall and then we'll copy and paste it down here right we have to do it here when he walks off a ledge so in both instances he'll be able to change direction now let's try it out now our enemy changes directions after hitting a wall and detecting a lance right right now he's on the ledge he's going back and forth right so we know the ledge works what we can also do is we can put him let's put him someplace else so let's put him over here okay so that we know he can detect walls right at least that's looking pretty good all right we'll even try one more thing well have them detect a Ledge or a wall at the same time so put him right here right there's a Ledge and the wall over here and a wall here and a Ledge so I have him do that they'll just he he goes crazy right that that looks absolutely ridiculous but he's doing what we programmed him to do so that part of it is great right let's just put him back up here for now right that's a little bit too spazzy for me so now our enemy change direction after hearing a wall detecting a Ledge fantastic some of you might want the enemy to walk straight off the ledge right that sounds great if that's the case then you can feel free to just skip the whole part about adding the raycast to the enemy and all the code that came after that then your enemy will be able to ping pong between the walls and maybe chase the player down some stairs today we created our first enemy and gave it a couple of simple behaviors although simple these behaviors are the bread and butter and a lot of platform games and you should get a lot of usage out of them using your creativity you can take the knowledge you gained about raycast and then implement additional conditions or behaviors maybe you could have the enemy run away when he sees a player right you could have the raycast shoot forward look for the player when it hits he could run away or when the enemy sees the player he could run straight at the player even faster and then launch some sort of fancy attack the possibilities are endless so that'll do it for today I like to thank you all for watching and I really hope this helped you out if you liked today's video please give it a like and hit that subscribe button if you haven't already maybe share this video with a friend and then we can all make awesome games together as always the project folder for today's episode and all other episodes in this series will be available for download on my patreon page you simply download the zip file extract the project folder and you'll have exactly what I've shown in this video here today so if you want to check that out and support the channel the link will be in the description and with that we'll call it a day so thanks again to everybody for watching and I'll see you in the next one real soon take it easy
Info
Channel: UmaiPixel
Views: 52,322
Rating: undefined out of 5
Keywords: Godot, pixel art, platformer, game engine, game development, indie
Id: 4HLUu_CB1Kw
Channel Id: undefined
Length: 23min 7sec (1387 seconds)
Published: Fri Sep 21 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.