Godot Top-down Shooter Tutorial - Part 6 (Making an Enemy)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone my name is Joe and welcome to part 6 of the Godot top-down shooter tutorial in this video you're gonna learn how to create an enemy how to handle bullet collisions with the enemy and then how to handle it when that enemy dies let's get started so if we come into our player scene will notice that pretty much everything here is something that we want on our enemy right we still want a sprite collision shape still need to know where to fire bullets still need muzzle flash etc so the first thing we want to do to create an enemy is actually copy our player so I'm gonna come into our main scene and I'm gonna right click on our player and hit duplicate you can also use command D or whatever hotkey it is and I'm gonna call this enemy now our enemy is still referencing our player scene so in order to change that I can right-click on it come down and hit make local and this will basically copy the player scene tree but make it a unique instance here and then I can right-click on this again and hit save branch's scene to actually save off our own enemies scene that currently has the exact same structure as our player and so now I can move our enemy over here and we'll see as expected it still looks identical to the player so the first thing we'll do is click on our enemy scene and we will change our sprite to use this blue guy down here since he still looks kind of like a combatant I'll move this over here so that we can change our sprite and save that and now if I come back to our main scene we'll see that we've got an enemy which is great so if I run this now we're gonna see two issues one is that the enemy moves with the player the reason for this is because we never actually changed the script that our enemy scene was looking at and two is that our bullets don't actually hit our enemy because we haven't built in that handling yet so those will be the next two things that we tackle so first we're gonna go over to our enemies scene and we're just gonna right click and hit detach or clear script you can also hit this button right here they both do the same now we're gonna add a new script just called enemy Gd one quick note is that we're getting a little unruly I mean it's not too bad yet but we're adding everything to our root directory so I think the next episode is actually going to be refactoring both our code and our directory structure to make that a little bit more nice and hopefully give you some ideas about better ways to structure your Godot project okay so I'm actually gonna leave our enemy script we just created for now and come over to our bullet so right now we don't have any handling for when our bullet enters another body and we can do that by coming over to the node and we'll do this through the UI and we're gonna connect the body entered signal and so I can double click on this and then we'll just use the default method name we'll hit that and now whenever our area is moving our bullet and it collides basically with another node with another body then we're gonna get this method fired off and one important thing to note is the difference between body entered and area entered if you have multiple areas that you want to check collisions so like say we had other bullets since we're using areas for bullets then you would use area entered area exited etc but because our player in our enemy our kinematic bodies their physics bodies we want to use body entered and so that's why we connected that signal and so in here we're gonna actually add our handling for what happens when our bullet hits the player the enemy our actual handling is gonna use something called duck typing and duck typing is a concept in programming that comes from the phrase you know if it talks like a duck if it walks like a duck if it quacks like a duck it is a duck but what it really means when it comes to code is by saying we don't actually have to know the exact type or class that something is we can just check you know does it have functions that that kleiss class might have and so what that means is we don't actually have to check our body coming in whether it is of a player type or an enemy type we can just check which functions it has and the nice thing about this is it means we don't have to use inheritance or we don't have to force anything that a bullet should hit to be you know some kind of an actor class or a player class or whatever we can add handling it's really anything in our game by doing this and it means that we can have one kind of common entry point one common function name that anything that a bullet should interact with can just implement and so what we're gonna do you is just say if body dot has method and we'll say handle kit and then we'll put our handling in here so again just to recap anything that should be hit by a bullet all they have to do is implement this handle hit method and then what we'll do here is just call handle hit so we can say body dot handle hit and we won't get type completion but we do know it's there since we since we're getting within this if statement and eventually we can add parameters here we'll kind of figure out what each body needs like what information it needs to handle a hit but yeah this is nice so this is what duck typing is right we haven't we're not checking what type the body is we're just checking what properties it has what methods it has and so this is a little bit more flexible and a little bit nicer than using inheritance or you know inheriting from some base actor class or bass player class etc now that we have our handling from the bullets side implemented what we need to do is actually add something to our player into our enemy scripts that will use this method this handle hit so we'll implement it there and actually add some handling I'm real quick I'm going to get rid of this output tab so this is kind of big but let's start with our player so I can come in here and I'm gonna add a new function which would be called handle hit and right now we don't have it taking any parameters but we'll just do that and so what I'm gonna do here just as a temporary measure we're gonna refactor this later but I'm gonna add a health variable will just say var whoops bar health and equals a hundred for now and what I'll say is just say prints player hit just so we can see what's happening I'm also gonna do help my sequels just do 24 now and then we can print out help okay great so we're not really doing anything right now we're decrementing health but that variable doesn't really do much but I'm gonna grab this function and come over to our enemy and pretty similarly we're gonna just add a variable called help pretty much the exact same thing that we had before and we will just decrement it here and say enemy hit and so we'll just do this to make sure that we're actually handling collisions correctly and they're being detected right so we'll run this in just a sec to see how it's looking but one last thing we want to do now is that once our bullet has hit something we actually want to cue it free so we were already doing this with our kill time our timeout so basically now we have two conditions it either goes long enough it doesn't hit anything and we just kill it or it hits something and then we kill our bullet anyway eventually we'll probably add a common method that can be called by both of these to do some other handling like you know an impact sprite or some other things but for now this will work and now I can save this and we can run it and we'll see that when we shoot our enemy we're actually getting enemy hit and obviously the health is going negative but it looks like it's working we can attack our enemy and it's our bullet is getting destroyed so the last thing we can do just to kind of give some sort of a game feel to this is just actually get rid of our enemy once we've lured its health to zero so if I come to our enemy scene and open up our script here now instead of just printing this out I can do if how let's do less than or equal to zero doesn't really matter then we can say enemy or we can just say Q free and now if I try playing this we'll see that if I hit our enemy enough times they'll go away so this is great guys we've made really simple mechanics but ones that feel good they look good I'm still I'm still excited about that muzzle flash and how that looks um but this is really good so thanks so much for sticking with me I hope you will subscribe if you like this so you can get notified with any of the new stuff that I'll be putting out and I'll see in the next video [Music] you [Music]
Info
Channel: jmbiv
Views: 5,213
Rating: undefined out of 5
Keywords: godot, godot engine, godot 3.2, godot tutorial, godot top-down, godot 2d, top-down shooter, how to make a game in godot, game development, game development tutorial, game development for beginners, godot for beginners, how to make a top down game in godot, how to make a top down shooting game in godot, game dev, indie game dev, indie game development, how to make video games, how to godot, hobby game development, gamedev, godot game engine
Id: tbS-_GhFyeY
Channel Id: undefined
Length: 8min 58sec (538 seconds)
Published: Sat May 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.