Creating a Hitbox in Unity
I have all of the attack animations set up for the player to attack in my game. Now I need to create the functionality to allow the attack to hit the enemy. In this article, I will go through how I create the hitbox for the player attack.
To start, you create a basic 2D sprite and change the sprite renderer to show a UISprite.
Now add a 2d box collider to the hitbox and mark it as a trigger.
Go to the player sprite and select the attack animation.
Make sure the hitbox sprite is a child of the player sprite so you do not have to create a new animator for the hitbox.
To create the hitbox you will want to hit record and for each frame align the hitbox 2d sprite with the position of the sword.
Once the hitbox is aligned, you can turn off the collider for the frames where the sword should not do damage, like after the swing is done. You will also want to delete the sprite renderer for the hitbox because it should only be the box collider.
This will give you a nice-looking hitbox for the sword swing.