Unity Guide

Implementing hitbox detection for melee combat | Unity

A quick guide about how to implement a system to identify hitboxes during a melee combat in Unity

Fernando Alcantara Santana
Nerd For Tech
Published in
4 min readOct 15, 2021


Objective: Implement a system to detect the hitbox of damageable gameobjects during a melee combat in a 2D game with Unity.

In the last post I covered how to implement an IDamageable interface in Unity. Now, it’s time to implement a system to identify the hitboxes of damageable gameobjects during a melee combat in order to apply damage to them.

If you haven’t seen the last post please take a moment to check it out as it’s an essential component for the hitbox detection mechanic:

Attack point

In order to start developing our hitbox detection system let’s create a new gameobject within our player that will represent the origin of the sword attack:

If we play the attack animation we’ll see that the point is at the center of the attack:

Enemy layer

Then, in order to maintain order and simplicity when identifying the hitboxes, let’s create a new layer for the enemies of our game:

Attack range

Next, in order to determine the range of our attack, let’s open the player script and create the next variables:

  • Attack point

This variable will store a reference to the attack origin Transform in order to know the position of each attack.

  • Attack range

This variable will determine the range of our attack starting from the attack origin.

  • Attack mask

This variable will indicate in which layers we’ll identify colliders when executing an attack.

In case that we want to visualize the attack range, we can make use of our variables within the Gizmos.DrawWireSphere method at the OnDrawGizmos method:

Once we assign the respective values for the variables through the inspector we’ll be able to visualize the attack range at the Scene view:

This way, everything inside our range will be affected by our attack when we execute it:

Identifying the hitboxes

And now, in order to identify the hitboxes inside our attack range, let’s create a new method in the player script where we’ll:

  • Store all the colliders within the attack range by using the Physics2D.OverlapCircleAll method with the respective parameters (attack origin, range and layer mask).
  • Search for the IDamageable component in each collider to know if it’s damageable.
  • If that’s the case, call the Damage method from the component to apply the respective damage.

If we take a look at the Damage method (that is implemented in the enemy script thanks to the IDamageable interface) we’ll see that it subtracts a unit from the health and triggers the respective animations:

Finally, in order to execute our Attack method, let’s open the Animation window and call it through an animation event at the exact frame where the sword is attacking in the attack animation:

If we run the game in Unity we’ll be able to see that the attacks are executed with the expected result:

And that’s it, we implemented a system to detect hitboxes during melee combat in Unity! :D. I’ll see you in the next post, where I’ll be showing how to create a loot system in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D



Fernando Alcantara Santana
Nerd For Tech

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).