How to set up a 2D sword hitbox in Unity

Niklas Bergstrand
2 min readJul 3, 2021

Let’s go over how to set up a 2D hitbox for the sword animation.

First, select the player and add a sprite as a child object. Select a suitable sprite and add a box collider to it:

Add the new object as a child of the player graphics to ensure we can use the same animation:

Select the frame in the attack animation where you want the attack to start and align the collider:

Start recording. Go through each frame and move the collider along with the sword:

When done with the recording remove the sprite component and set collider to IsTrigger:

Add a RigidBody2D and turn off gravity:

Record three more frames, one with the collider turned off and another with it turned on and then the last have it turned off again. Disable the collider on the hit box:

To avoid the hit box detecting the player collider you can add a layer to it and under Project Settings-Physics 2D untick collisions between the Sword and the Player layers:

Good luck!

--

--