Add hitbox to built in weapon on 2D sprites

Daniel Kirwan
2 min readSep 8, 2021

--

Objective: Add a hitbox to a 2D sprite that has a weapon built into the sprite

When working with 2D sprites you sometimes have characters or enemies that have weapons built into the sprites. I ran into this issue with a 2D game project, so I needed a way that I could add a hitbox to the sword as it is swinging.

I added a 2D sprite to my main character sprite in the Unity editor, I used a standard sprite provided by Unity. Click the circle next to the empty sprite and scroll right down to the bottom and select UI sprite. Next, add a box collider 2D to the new sprite. I renamed my UIsprite to hitbox.

Open up the animator that is controlling the main sprite and play your attack animation. Click on the first frame in the animator and then position the hitbox and rescale the box collider to fit around your weapon.

You can see above I have moved and scaled the box collider to where the sword appears on the first frame of the attack animation.

Now comes the tedious part. Click record mode and then move to the second frame, then move and rotate the box collider to fit around your weapon. You will need to do this for every frame of the animation. Once you have adjusted the box for the final frame, you can click the stop record button.

You can now press the play button on the animator window and you should now see that the hitbox is moving with the weapon.

That’s it for adding a collider to an inbuilt sprite weapon.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet