2D Mobile Game: Player Hit Box

Ryan McCoach
4 min readApr 12, 2022



In this article, we are going to cover how to add a hit box to an attack animation that will be able to ignore the player but detect other objects in the scene.

Hit Box Setup

We start by creating a Sprite that is the Child of the Sprite of the Player. This is important because the Player Sprite has the animator, so we are simply adding to Attack animation already in this animator instead of creating a whole new animator if the Hit Box was its own game object.

A Box Collider is added to the Hit Box and Is Trigger is set to true. This will allow the Hit Box to pass through other colliders.

A Rigidbody 2D is needed to be able to detect other colliders that the Hit Box will come in contact with. Make sure the Gravity is set to 0, so it doesn’t start to fall.

It is time to track the Hit Box with the Sword in the Attack animation. We open up the Animation window and key the first frame. We resize and reposition the Box Collider to fit the Sword.

It is time to record the first keyframe for the Hit Box, we hit the record button and adjust the rotation of the Hit Box to register that first key frame.

We go through each of the Attack frames and adjust the Hit Box to track the movement of the sword.

Once we are done, we should have the Hit Box follow the Sword through the forward motion.

We Hit Box Box Collider is present even when the animation is not present and we only want it to be active when the Attack animation is playing.

Deactivate the Box Collider as the default setting.

We hop back into the Attack animation and enter record mode, and on the first frame we enable the Box Collider and stop recording.

This will now turn on the Box Collider when the Attack is played and turns it off when it is done.

Hit Box Script

We will create a script (Attack) and attach it to the Hit Box.

Using OnTriggerEnter, we can detect what other Colliders the hit box it hitting. Using a Debug.Log we can get the name of the other Collider the hit box is touching.

We run into an issue when we test this and it we can see the Hit Box Collider is detecting the Player’s Collider. To remedy this, we can create a layer for the Sword and have it ignored by the player.

Sword Layer

We are going to add two more Layers, one for the player and the other for the sword.

We will add the player layer to the Player

We will add the Sword layer to the Hit Box.

In projects settings under Physics 2D, we can select which layers we want to ignore and in our case we want the Player layer to ignore the Sword layer.

Running the game again, we no longer see Log message detecting the Sword hitting the Player Collider. Now, we can move forward with detecting Enemy Colliders, but that will be another time until then…Cheers!