Learn to code melee hitboxes and hurtboxes in Godot using GDScript to create damage-dealing and damage-receiving nodes.
Ask about this video. Answers come from its transcript only — with the timestamp, so you can check them.
Generated from the transcript and can be wrong — check the timestamp.
Key Takeaways
- Hitboxes and hurtboxes are essential for melee combat mechanics in Godot.
- Collision layers and masks must be carefully set to filter interactions between hitboxes and hurtboxes.
- Extending Area2D and using custom scripts allows flexible and reusable hitbox/hurtbox components.
- Signals and type safety help prevent bugs during collision detection.
- Duck typing enables generic damage handling across different enemy or object types.
What the video covers
- Introduction to hitboxes and hurtboxes as collision shapes for melee attacks in Godot.
- Creating custom nodes for hitboxes and hurtboxes by extending Area2D.
- Setting up collision layers and masks to ensure hitboxes detect hurtboxes and vice versa.
- Coding a hitbox script with base damage and collision properties.
- Coding a hurtbox script to detect hitboxes and handle damage reception.
- Using signals and type checking to safely detect collisions between hitboxes and hurtboxes.
- Applying duck typing to call a take_damage function on the owner node of the hurtbox.
- Adding hitbox and hurtbox nodes to sword and enemy scenes respectively.
- Configuring collision shapes like CapsuleShape2D for hitboxes.
- Encouragement to download project files to follow along and extend the code.
Chapters
Full Transcript — Download SRT & Markdown
Speaker A
In a previous video, we saw that games rely on two kinds of collision shapes to make melee attacks feel great: hitboxes and hurtboxes. Hitboxes deal damage, while hurtboxes receive damage. In this video, you will learn to code both in Gdau.
Speaker A
we'll start by coding the hitbox then the herd boox and we'll use them to deal damage to enemies this video is sponsored by our course learn to good from zero with gdau more on that at the end of the video in the description
Speaker A
We'll start by coding the hitbox, then the hurtbox, and we'll use them to deal damage to enemies. This video is sponsored by our course Learn to Gdau from Zero with Gdau. More on that at the end of the video in the description.
Speaker A
we need to do is to create new kinds of nodes that represent hitboxes and her 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 CR a I want to be able to uh
Speaker A
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 sprites that I'm using here. So if you want to follow along, I invite you to download it.
Speaker A
click on the practice folder click new script and enter a name like my headbox 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
Speaker A
What we need to do is to create new kinds of nodes that represent hitboxes and hurtboxes. I'm going to double-click on my sword 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 MyHitbox, 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 Area2D. Areas are the basic kind of node that detects physics interactions in Gdau, 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 hurtboxes. Then, to create nodes based on this script, we have to create a new line that uses the class and theore 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 MyHitbox, 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 of our hitbox because the hitbox is responsible for dealing damage, so it should hold the damage. 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 Area2D node to talk about physics layers and masks. If you go to the inspector on the right and expand the Collision category, layers and masks appear. These control what this node will collide with, and for our hitboxes and hurtboxes, what we want is for them to just detect one another precisely. We want hurtboxes 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 two and to have no mask. Why? Because the layer represents the category of physics objects this node is a part of, and the mask represents the category of physics objects it will detect. That's the general idea between 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 two. 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 two. You want to assign the value two 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 one, the value is two, but it works with powers of two. So bit three, or bit two rather, would be value four, and so on. Then it gets to eight, 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 zero, which will turn off all the Collision masks. So that's our hitbox script done. Then we need the hurtbox, 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 MyHurtbox. This one is also going to extend from Area2D, and we're going to give it a class name of MyHurtbox 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 zero, and Collision mask is going to be equal to two. This will allow this node to detect MyHitbox. Then we will use a signal so that when a hitbox hits a hurtbox, we detect that and deal damage. I'm going to add the ready function and call connect area enter. I'm going to connect it to this node, and I'm going to call on area entered. When a hitbox hits this hurtbox, 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 MyHitbox. Why do we do that? Well, this will make it so if anything other than a node of type MyHitbox 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 hurtbox 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.take damage, and we're going to pass the hitbox do damage value to that function. This is called duck 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, monsters, or anything, define that take damage function to attach a hurtbox 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 and hurtbox to the sword and enemy. So open the Practice Sword scene, practice_sword.tscn, and there select the sprite, and we're going to add the MyHitbox 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 CollisionShape2D as a child of the node. Then in the inspector, click the empty slot next to shape, and we're going to use a CapsuleShape2D. 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. Then you can click and drag the capsule to cover the 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 melee 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 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 e...
Speaker A
perfect for things like hitboxes and her boxes then to create nodes based on this script we have to create a new line that uses the class and theore name keyword followed by the name we want for our node and as soon as you've done that you
Speaker A
can already go in the editor I press crl a and search for my hitbox and you'll see the node is right there all right so let's keep coding uh we want to do two things first we're going to create a
Speaker A
variable to represent the base damage of our hitbox because the hitbox is responsible for dealing damage so it should hold uh the damage and then we're going to change the default properties of that n and that's why we're making a
Speaker A
script well keep 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
Speaker A
layers and masks appear these control what this node will collide with and for um our hitboxes and her boxes what we want is for them to just detect detect one another precisely we want her boxes to detect hitboxes and that's it and so
Speaker A
typically you change these layers in masks um 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
Speaker A
right um and so what we want precisely is for the hitbox to be on two and to have no mask why because the layer represents the category physics objects this node is sp of and The Mask represents the category
Speaker A
of physics objects it will detect that's the general idea between uh 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 Two now you have to be careful
Speaker A
about that because Collision layers and masks work with binary numbers and so um you want to set it to Layer Two you want to assign the value two to the Collision layer property now the way you know which value to use is by hovering over
Speaker A
the Collision layer you can see it says bit one the value is two but it works with powers of two so uh bit three or or bit two rather would be value four uh and so on then it gets to eight uh and
Speaker A
uh 16 and so on right and for the Collision mask sorry I'm going to get back to my hbox script and for the Collision mask we want a value of zero which will turn off all the Collision masks so that's our hibox script done
Speaker A
then we need the herd boox 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 herbox this one is also going to extend from
Speaker A
area 2D and we're going to give it a class name of my her box to be able to create nodes based on this script okay so this one will work a bit differently uh we're going to create an
Speaker A
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 zero and collision mask is
Speaker A
going to be equal to two this will allow this node to detect my hitbox then we will use a signal so that when a hitbox hits a her box we detect that and deal damage I'm going to uh add
Speaker A
the ready function and call connect area enter I'm going to connect it to this node and I'm going to call on area entered when a hitbox hits this her box and you're going to see a couple of interesting tricks here um I'm going to
Speaker A
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
Speaker A
be my hitbox why do we do that well this will make it so if anything other than and a node of typee my hitbox touches this area the hitbox will have a value of null automatically and so we can safely
Speaker A
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
Speaker A
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
Speaker A
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 um do things like this if the owner has a function
Speaker A
has a method called take down damage uh then we're going to call it like this owner. take damage and we're going to pass the hitbox do damage value to that function uh this is called dock typing uh no matter the kind of node that is at
Speaker A
the root of the scene as long as it has a function called take damage we can call that function right uh and we won't get errors hopefully this allows you to create crates uh monsters or anything Define that take damage function to
Speaker A
attach a her 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 and her box to the sword and enemy so uh
Speaker A
open The Practice Sword scene practice sword. tscn and there select the Sprite and we're going to add the mod hbox node as a child of it because the sword needs to deal damage it's a regular area and it needs a collision
Speaker A
shape to function so press crl 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 um I'm going
Speaker A
to rotate the capsule you can press e to select the rotate tool click and drag with the control key down to rotate by uh fixed increments and then press Q to go back to the selection tool and click
Speaker A
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
Speaker A
this is a thing we do often in games because that way the uh Collision box is going to follow the Sprite or even the 3D me games like Dark Souls do things like these now uh what games also do
Speaker A
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 um it may miss enemies at times and the players might get frustrated so if you make the shape
Speaker A
a bit larger uh it's going to be a bit easier to hit enemies and we have animation player in there uh with a slash animation that is going to play Whenever you press the space bar when running the practice scene and so that
Speaker A
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
Speaker A
press control a and add a my herbox node as a child of that and there again we need a collision shape for a to function so press contr a collision shape 2D and we're going to add a capsule
Speaker A
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
Speaker A
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 enemy node so select the
Speaker A
node click the add script icon and create the script uh there we're going to use our animation player so um we're going to start by creating an unready variable called animation player to store the animation player node and remember that our herbox is
Speaker A
going to try to call the take damage uh function on our enemy the owner of the scene so we can define a function called take damage it should take an amount uh value that's going to be a whole
Speaker A
number and there we can just call animation player. playay hit we prepared a hit animation on the enemy if you want to see the damage you can also call a print uh and we can write damage a comma
Speaker A
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 W ASD and when you press space you're
Speaker A
going to attack the enemies now if you walk over the enemies you'll see them take damage as well why is that but it's because we need to disable our hitbox um whenever we are not at attacking and we
Speaker A
typically do that in the gamees animation so go back to Practice Sword select the animation player and the SL 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
Speaker A
top left in the the gray area uh pull the cursor to the start of the animation select the Collision shape node and then we're going to create a key frame next to the disable Bol property this is going to offer to create a reset reset
Speaker A
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
Speaker A
animation and then uh right click in the animation track for the Collision shape click insert key select the key frame and click on the checkbox next to Value to disable the shape again and if you play the animation you will see at the
Speaker A
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
Speaker A
animation that was automatically created for us it stalls the default state of nodes um you want to click the disabled key in the Collision shape uh track and in the inspector you want to turn it on you want the shape to be disabled at the
Speaker A
start of the game this animation will automatically reset your sword uh when starting the game right so we can now play the scene and press uh space to attack and only when you are attacking will you damage the enemies and this is
Speaker A
how you create La attacks in gdo now you know how to code attacks in your gdo 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
Speaker A
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 zero with gdo
Speaker A
Will 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
Topics:GodotGDScriptmelee attackshitboxeshurtboxescollision detectiongame developmentgame programmingArea2Dcollision layers











