How to Make Melee Combat in Godot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you're going to learn everything you need to know to implement a melee combat system in your godot game from handling collisions to using sprites to animations and it's super easy quick and the system we make will be very scalable and easy to implement other weapons and types of damage in the future so you see here that when i move closer to our enemy every time i click we swing and our enemy loses health and when their health goes to zero they disappear so we'll cover all this in this quick video and you'll be well on your way to your own combat system let's get started so i've already got a 2d scene set up here and i'm going to show you what i have and we're going to start adding some scripts to this so we can see it in action so i've got two things right now i've got a player and i've got an enemy and if we look at our melee player you'll see that it's just a kinematic body 2d so it's got a collision shape 2d and it's got a sprite which is pretty standard and for this i'm using kenny's roguelike character pack i'll have that linked in the description below but so we've got this and there's one other interesting thing that i have here besides just our player itself i've actually got a weapon and this is something that is totally separate from our player and so i made our weapon a area 2d and it has a couple things again it's got a collision shape and a sprite but notice that our collision shape is actually in front of the weapon so our collision shape here represents the hitbox of our weapon when it swings so whenever our weapon swings we want our collision shape to be where our our area of hitting will be so when we swing anything within this this collision shape is what our weapon is going to hit so that's why you'll see that even though our weapon sprite is right here our collision shape is in front of it because that's where our swing is going to be and i've added an animation player to our weapon with a swing animation and you'll see when i play this if i start from the beginning that our weapon swings and covers most of this box it's actually if we look at the height of it it's not really getting this far part of the box so i'm gonna bring this in just a little bit so that our weapon is a little bit more true to form we probably want it right here so now if i play we'll see that it's actually hitting pretty much the majority of that swing i just bring that down whoops i might just there we go so i think that should be good there we go so let me break down what i've got in this animation player here so we've got our sprite track which is our weapon sprite it starts at the upright position and ends at the upright position so these are the same keys but then in between i've got it where it moves forward a bit and rotates to be flat and you'll notice that this is a um a uh this this animation lasts for 0.4 seconds what i have now is it's the same amount of time going down as it is coming back one thing you could do is bring a whoops bring the down part of the swing a little bit closer to the the beginning of the animation than the end so that the swing down is a little bit faster and more dynamic than coming back up and so it just adds a little bit more dynamism to your swing animation but you can adjust as needed we have a pretty short animation here so it looks pretty fine regardless so we've got our sword that's been rotated and moved forward but there's also another track that we have going on here and this is our collision shape so you'll notice that i've actually disabled our collision shape by default on our swords hitbox but in this animation i am enabling it i'm turning off disabled once the swing starts you'll notice i don't do it right at the beginning because i kind of want the swing animation to be a little bit in play i actually want it to be going down a bit before we enable our collision shape but once our swing is happening all of a sudden our hitbox is enabled and it's going to start detecting collisions and it's going to detect collisions for the entirety of the swing so if there's something that is not in that box right here but enters it just a few milliseconds later it will be picked up and so this is how you can make a swing animation that will enable a collision shape that was disabled before and give you a nice little effect like that super easy this is all using kenny's free assets you don't need you know to do anything really fancy in a sprite or make your own pixel art for example you can make something like this just just with basic um even even with assets that don't have animations so this is um you you're you're seeing the way your struct your your player and your weapon are structured in your own game might be a bit different but this should give you a starting point for how to actually make a use an animation player to detect collisions with a melee weapon and now what we're going to do is actually add a script to our player into our enemy to actually handle those collisions and do something about it when our player hits the enemy so first let's start by closing this and then what we're going to do is add a script to our player so i've got it's called melee player we'll just add this and here what we're going to do is just say function unhandled input and this is the function you want to use pretty much any time you're you're doing input in your game that isn't for a gui element and we really care about we want an event for clicking for attacking so i'm going to come into our project settings into our input map and i'm going to add an attack action and then i'm going to make this attack be mouse button and then left button and this is important you see i've already done this for another thing but we want um whenever the player clicks left that they attack you could also add a joy button if you're using an xbox or ps4 controller or ps5 um but we'll just do the click for now so we'll say if event dot is action pressed and attack so if we've attacked we want to say we'll have to grab it here so we'll say i'm ready var weapon equals weapon so this will be we'll grab a reference to our player's weapon and we'll say weapon.attack now we don't have a script on our weapon right now so this is going to fail so we'll actually need to add a script to our weapon so i'll come over here click the script button on weapon and we will say function attack and this is what's going to do it so we're going to need to do a couple things here we need to actually uh do our swing animation so we'll say var and m and this is just going to be our animation player and we'll do anime.play swing and so now at the very least what we should see is when the player clicks that the weapon plays the swing animation so if i run the scene and i click it's way up there but you'll see that our player is actually doing the swing animation which is wonderful so we've got a way to do melee combat now we just need a way to detect when we've collided with something one other thing we're also going to need too before we get going is to add some movement capabilities to our players so real quick we'll just do that i've already actually added a little bit to that to our script so if we come in here you'll see that i've added a physics process function here and then i've got a variable called movement direction which is a vector 2 and there's a lot going on here so it's important that you get the parentheses correct so vector 2 and then i've started a new line just to keep it more legible and so the x portion of our vector is action strength right minus input dot get action strength left and what that means is i've gone into our input map again in project settings and you'll see i've added an up left right and down which are just w a s d standard controls you can do arrow keys or joystick buttons whatever you need but so our x portion of our movement direction is the strength of our right motion minus the strength of our left motion and the reason we're doing get action strength and not is action pressed is because get action strength will actually return a value between 0 and 1 when you're using a joystick like on a game controller a gamepad of some sort if you're using a keyboard wasd it'll either be zero or one so it works totally fine on keyboard but it also by default gives you joystick capabilities right out of the box so whenever you're doing movement and there's a chance you might want to use a joystick down the road or support that support some kind of a controller this is pretty much the best way to do input and it also avoids all the if statements and messiness so this is the best way to do input but you'll see so our x is right minus left so we're getting the strength of our right direction minus our left so if they're both pressed it'll just be zero and then we're actually getting for our y component our down action strength minus our up action strength the reason it's down minus up is because going downward in godot is actually going increasing in value on the y-axis so we flip them here but notice that this is movement direction it's not speed or velocity so once we get this vector we're normalizing the entire thing so it'll become a unit vector now what we need to do is actually say export and we'll just say float variable speed and we'll say 200.0 for now so this is the speed we're going to move and now we can actually insert the line of code that will move our character and we're going to use the built-in kinematic body function move and slide we're going to take our movement direction multiply it by our speed and so this is going to actually move our player okay so we're getting ready to run it you'll also see i've added a camera just to zoom it in a little bit more so we're not so far away but so now if i run our scene do command r if i hit wasd we can move our player around which is wonderful and if i click we'll see we are attacking still now we just need to actually register these attacks and there's some weird stuff going on with the draw order of our sprites but we can fix that later on so now that we've got this we can come into our player or into our weapon and what we can do here is we can actually connect on our weapon the body entered function on our weapon so make sure when you connect this you're connecting your weapons body entered signal here and you're going to connect it to the script on your sprite so just go over that again or on your weapon sorry select your weapon area 2d come over into node and signals double click the body entered signal to connect it to your weapon script and what we're going to do here is say if body so you'll notice that we're getting a body in this is the body kinematic body physics body whatever it is that the weapon is hitting so if body and then we'll say has method this is something called duct typing it lets you kind of make sure that what you hit can handle our weapon hitting it you know it has a certain interface that we can use without actually having to check what type of object our body is it gives it much more flexibility than trying to make it some rigid class so we'll say handle hits so if our body has a function on it to handle hit we will say body dot handle hits now in order for this to happen we actually have to make sure that our body that we're hitting has this method so we need to come into our enemy add a script and we'll just call it that and i'm just going to add a function here called function handle hit it's important that this matches exactly the name of the function that you specified in your melee player script here in our weapon.or uh sorry in our weapon script this handle hit needs to be the same name that we have on our melee enemy and here all we're going to do is print and say enemy was hit and so now when i run our game we should see two things one is that when the player because our weapon's collision shape is disabled when the player just walks up to the enemy we should not see a collision registered because this is disabled but when the player is near the enemy and clicks and attacks we should see the enemy get hit because our collision shape is now enabled so we should see it's working in both of those ways to help with this i'm going to go up to debug and hit visible collision shapes and this will show us all of our collision shapes while our game is running so if i hit command r here's our game you can see the collision shapes and you can see when i attack all of a sudden we have a our collision shape for a weapon turns blue because it's now enabled and if i whoops let me uh just make this smaller so we can see our output so you'll see if i walk up to our enemy we do not get any print outputs but when i attack we see enemy was hit and every time i attack we see enemy was hit and so there you go now we have the very basics of melee combat working in our game there's obviously so much more you can do and we'll do just a tiny bit more in this video but making melee combat that works and looks good and is has feedback for the player and everything you need is really easy to do in godot so the last thing we're going to do is just add some health to our enemies so if i come into our weapon here i can say export and we'll say this is int and you'll save our damage and this is going to be we'll say it does 10 damage and we will pass in damage whenever we call handle hit so whenever a weapon hits a body it will tell it how much damage that weapon is doing and then now in our melee enemy we need to have something like export uh we will say ins var health and we're making these export variables so we can change them in the editor in case you need to and we'll give we'll give our enemy 30 health and so now all of a sudden remember in our weapon when we call handle hit we are passing in a parameter so we need to add this damage parameter to our enemies handle hit so this is going to be damage it was an integer and we'll say enemy was hit what we're going to do is say health minus equals damage and we'll say if health less than zero then we will queue free which is just going to delete our i'm just going to delete our enemy so we'll say enemy was hit current health and then we will say um plus health so what's going to happen is whenever our enemy gets hit uh oh whoops i need to do string just because our health is an integer i need to convert it to a string here so that we know how to print it out so what's happening here is whenever we hit an enemy they're going to lose that amount of health that our weapon takes away the they're going to lose in health the weapon's damage we're going to print out their new health and if their health goes under zero they're going to disappear they're going to die obviously you'd have a more complex death system in a real game but just for purposes of demo this will be great so now when i come up to our enemy and attack him we'll see his health is 20. now it's 10 and now it's zero oh we we probably need to do less than or equal to not less than but if i hit him again we should see him disappear and just like that you have melee combat and enemies that die more or less so we'll say left less than or equal to zero because if you're at zero health then you're you're should probably be dead anyway so there you have it everyone we have melee combat that works it's really easy to implement and we've done it in a nice way with this kind of modular weapon system where you can swap in different weapons with different damages you know you could have tons of different weapons with different sprites and damages in your game really easily and it just kind of works it's you don't have to do any fancy configuration and it's just a simple easy system so anyway thanks so much for watching everyone i appreciate it and if you found this video helpful a like and subscribe to support the channel is always appreciated we'd love to have you in our discord server the link to that is in the description you can talk more about the tutorials and ask any questions there and if you appreciate my work donating or buying me a coffee on buy me a coffee link also in the description is always appreciated helps me to continue making great tutorials thanks so much see you in the next video you
Info
Channel: jmbiv
Views: 6,132
Rating: undefined out of 5
Keywords: godot, godot engine, godot 3.2, godot tutorial, godot 2d, how to make a game in godot, game development, game development tutorial, game development for beginners, godot for beginners, game dev, indie game dev, indie game development, how to make video games, how to godot, hobby game development, godot game engine, godot melee attack, godot attack, godot action rpg, godot melee, godot rpg, godot rpg battle system, godot sword slash, godot collision 2d, godot 2d melee
Id: dBvfwLxoY3I
Channel Id: undefined
Length: 15min 59sec (959 seconds)
Published: Mon Jan 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.