How to Code Melee Attacks in Godot: Hitboxes and Hurtboxes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in a previous video we saw that games rely on two kinds of collision shapes to make melee attacks feel great hitboxes and hurt boxes hitboxes deal damage while hurt boxes receive damage in this video you will learn to code both in godot we'll start by coding the hitbox then the herd box and we'll use them to deal damage to enemies this video is sponsored by our course learn to code from zero with godot more on that at the end of the video in the description you'll find a link to download the project files i will be using in this video it contains the finished code for this project but also the sprite that i'm using here so if you want to follow along i invite you to download it what we need to do is to create new kinds of nodes that represent hitboxes and herd boxes i'm going to double click on my sort to open the corresponding scene and want to do something like this when i press ctrl a i want to be able to search for a hitbox node and just create it we have one done in this project but we're going to code our own from scratch to achieve this we have to create a script file so i invite you to right click on the practice folder click new script and enter a name like my head box which i'm going to copy and click create to create the script double-click the script file to open it in the editor and the first thing that we want to do is to set the type to extend area 2d areas are the basic kind of node that detects physics interactions in godot like the player is in front of a chest you press a key you open the chest but they are perfect for things like hitboxes and hurt boxes then to create nodes based on this script we have to create a new line that uses the class and discard name keyword followed by the name we want for our node and as soon as you've done that you can already go in the editor press ctrl a and search for my hitbox and you'll see the node is right there all right so let's keep coding we want to do two things first we're going to create a variable to represent the base damage or hitbox because the hitbox is responsible for dealing damage so it should hold the damage and then we're going to change the default properties of that node and that's why we're making a script we'll keep it simple in this video but you can build upon what we'll do here now let me create an area 2d node to talk about physics layers and masks if you go to the inspect on the right and expand the collision category layers and masks appear these control what this node will collide with and for um our hitboxes and herd boxes what we want is for them to just detect one another precisely we want herd boxes to detect hitboxes and that's it and so typically you change these layers and masks to filter the collisions in your game right we named the first layers in this project for you so layer one we called it the game world and layer two is going to be for hitboxes right and so what we want precisely is for the hitbox to be on layer two and to have no mask why because the layer represents the category of physics objects this node is part of and the mask represents the category of physics objects it will detect that's the general idea between behind these two so if i go back to my hitbox script i'm going to do these two things collision layer i want to set it to layer 2. now you have to be careful about that because collision layers and masks work with binary numbers and so you want to set it to layer 2 you want to assign the value 2 to the collision layer property now the way you know which value to use is by hovering over the collision layer you can see it says bit 1 the value is 2 but it worked with powers of 2 so bit 3 or bit 2 rather would be value 4 and so on then it gets to 8 and 16 and so on right and for the collision mask sorry i'm going to get back to my hitbox script and for the collision mask we want a value of 0 which will turn off all the collision masks so that's our hitbox script done then we need the hurt box the one that we will attach to enemies to receive damage for that right click on the practice folder again create a new script and we're going to call it my herd box this one is also going to extend from area 2d and we're going to give it a class name of my heart box to be able to create nodes based on this script okay so this one will work a bit differently we're going to create an init function and we want it to detect the hitboxes and to receive damage for that we're going to set the collision layers and masks to complement the hitbox we'll say collision layer is equal to 0 and collision mask is going to be equal to 2. this will allow this node to detect my hitbox then we will use a signal so that when a hitbox hits a hurt box we detect that and deal damage i'm going to add the ready function and call connect area entered i'm going to connect it to this node and i'm going to call on area entered when a hitbox hits this hurt and you're going to see a couple of interesting tricks here i'm going to define that function and it's going to receive an area node through the area entered signal now we can do better than that we can say i want to receive a hitbox and the type of that is going to be my hitbox why do we do that well this will make it so if anything other than a node of type my hitbox touches this area the hitbox will have a value of null automatically and so we can safely do something like if hitbox is equal to null we return from the function and avoid bugs this way and then if we have a proper hitbox we can deal damage there are a couple of ways we can go about that we could emit a signal but instead we're going to use the owner property the owner of a node is the top node of a scene it's in so if we go to the enemy scene and we create a herd box here no matter where we create it in the tree the owner will be the enemy node so it's common to use the owner property when you create nodes that work as components i'm going to do things like this if the owner has a function has a method called take damage then we're going to call it like this owner dot take damage and we're going to pass the hitbox dot damage value to that function uh this is called duct typing no matter the kind of node that is at the root of the scene as long as it has a function called take damage we can call that function right and we won't get errors hopefully this allows you to create crates uh monsters or anything define that take damage function to attach a herd box to them and then they can take damage and we're going to put that to good use in a second because we're going to now add the hitbox in a hurt box to the sword and enemy so open the practice sort scene practice soul.tscn and there select the sprite and we're going to add the my hitbox node as a child of it because the sword needs to deal damage it's a regular area and it needs a collision shape to function so press ctrl a and add a collision shape 2d as a child of the node then in the inspector click the empty slot next to shape and we're going to use a capsule shape 2d i'm going to rotate the capsule you can press e to select the rotate tool click and drag with the control key down to rotate by fixed increments and then press q to go back to the selection tool and click the handles to resize the capsule and then you can click and drag the capsule to cover sword now we created the hitbox as a child of the sprite because then we can rotate the sword and the hitbox follows this is a thing we do often in games because that way the collision box is going to follow the sprite or even the 3d mesh games like dark souls do things like these now what games also do typically is they make the collision shape larger than the sword why because when you attack and the shape or the the sprite rotates very fast it may miss enemies at times and the players might get frustrated so if you make the shape a bit larger it's going to be a bit easier to hit enemies and we have an animation player in there with a slash animation that is going to play whenever you press the spacebar when running the practice scene and so that will rotate the shape and the hitbox and deal damage to enemies well to deal damage to enemies we have to reopen the practice enemy scene or open it select the sprite node or the enemy whichever you prefer and we're going to press ctrl a and add a my herd box node as a child of that and there again we need a collision shape for it to function so press ctrl a collision shape 2d and we're going to add a capsule shape to that in the inspector and you can resize it to roughly cover the enemy like this you can click and drag over the shape to move it on top of clicking the handles to resize it with that we need a way uh to see when we deal damage to enemies because the shapes are going to collide but uh we're not going to see that on the enemy so we're going to add a script to the anime node so select the node click the add script icon and create the script there we're going to use our animation player so we're going to start by creating an unready variable called animation player to store the animation player node and remember that our herd box is going to try to call the take damage function on our enemy the owner of the scene so we can define a function called take damage it should take an amount value that's going to be a whole number and there we can just call animationplayer.play hit we prepared a hit animation on the enemy if you want to see the damage you can also call a print and we can write damage a comma and pass the amount and you'll see it in the output console then you can go back to the practice scene press f6 to run it and there you can control the sword with wasd and when you press space you're going to attack the enemies now if you walk over the enemies you'll see them take damage as well why is that well it's because we need to disable our hitbox whenever we are not attacking and we typically do that in the game's animation so go back to practice sword select the animation player and the slash animation in the bottom right click and drag on the zoom icon to zoom in on the timeline and we're going to click in the top left in the the gray area pull the cursor to the start of the animation select the collision shape node and then we're going to create a keyframe next to the disabled property this is going to offer to create a recite reset track and we can click create to confirm so at the start of the slash animation the collision shape will not be disabled it will be enabled and deal damage and we can move forward with time cursor to around the end of the animation and then right click in the animation track for the collision shape click insert key select the keyframe and click on the check box next to value to disable the shape again and if you play the animation you will see at the end the shape turns gray okay that's pretty cool now we also need it to be disabled at the start of the game and to do that we have to click the animation drop-down menu and go to the reset animation that was automatically created for us it stalls the default state of nodes you want to click the disabled key in the collision shape track and in the inspector you want to turn it on you want the shape to be disabled at the start of the game this animation will automatically reset your sword when starting the game right so you can now play the scene and press space to attack and only when you are attacking will you damage the enemies and this is how you create melee attacks in godot now you know how to code attacks in your good or games the code is functional but it doesn't feel very good that's why in the next video we will show you how to make attacks feel great you will see all the little things that we layer to improve the feel of combat in games this video is sponsored by our course if you want to become a game developer and you have little programming experience our course learn to code from xero with godot will teach you everything you need to know to make your own games it's the only course with interactive practices that you can do right in the engine you can find the link to the course in the description below
Info
Channel: GDQuest
Views: 85,214
Rating: undefined out of 5
Keywords: godot combat, hitbox, hurtbox, godot fighting
Id: JWjzSn95bM0
Channel Id: undefined
Length: 15min 11sec (911 seconds)
Published: Thu Aug 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.