Hitbox attack system in Unity 2D

Hernando Nieto Jaramillo
2 min readAug 29, 2021

--

The player has an attack animation. Now we will put a box collider in it to make it trigger with other gameobjects such as the enemies

  • Select the player and create a new sprite as a child gameobject called HitBox. It will be the sword’s collider.
  • Place the HitBox as a child of Sprite. I will use the orange color as reference, but this SpriteRenderer will be removed later.
  • Add a Box collider 2D
  • Select the sword’s attack animation and lock the animation’s view
  • Place and modify the HitBox as desired in the first frame
  • Start recording
  • Modify its values in each frame. In the last frame, disable the BoxCollider2D
  • Delete the HitBox’s SpriteRenderer component
  • Set isTrigger to true in BoxCollider
  • Add a Rigidbody2D and set the Gravity Scale value to 0
  • Create an Attack script and attach it to HitBox gameobject

Now with this HitBox and its script, we can implement an attacking mechanic for identifying if the other gameobject is an enemy, and if so, executing code such as reducing the enemy’s health and increasing player’s score

--

--

No responses yet