Setting Up a Hit Box for Weapons in Unity

Jon Jenkins
2 min readSep 24, 2021

Here we see our player with a sword:

When he swings his sword we would like to be able to detect if the sword hits something. We can do this by adding a hit box to the sword graphic.

First thing we need to do is add a sprite object under our player object. We will call this sprite hit box. We will also add a boxcollider2d to it and set it as a trigger. Once we get it positioned in our scene, we can remove the sprite renderer from the game object:

Now we will synch it with the sword attack animation. This is done by simply going through each frame of the animation clip and changing the position and rotation of the Hit Box so that it’s collider lines up with the blade of the sword.

Here I have already done this for most of the frames and will demonstrate on the last frame needed. Make sure the record button is activated in the animation clip:

After this is synched with every relevant frame in the animation, you should get a hit box that moves with the sword. As seen below:

Next thing you will want to do is add a rigid body 2d to the hitbox, with gravity set to 0. You will also need to add a script to the hitbox to determine what it hits and apply other logic based on the hit.

That’s the basics of getting a hit box set up in Unity.

--

--

Jon Jenkins

A Unity Developer, interested in all things Unity.