Enemy Hitboxes

Learning how to implement Enemy Hitboxes and making the Enemy face the player.

Simon Truong
4 min readDec 15, 2021

In this article we will focus on making the Enemy face the player when getting hit and attacking and also implement a Hitbox for the enemy to target the player.

Skeleton Script:

In the skeleton script, we are going to first understand when the enemy needs to flip the sprite image. Technically within Unity, we can calculate the player position and compare that position with the skeleton via a Vector3 variable. This variable’s value would fluctuate between positive when the player is right of the skeleton and negative when the player is on the left side. With that knowledge we can set up a If condition so whenever the value is positive we will not need to flip, and when the the value is negative we can use the spriteRenderer flipX method.

We will also add in another condition within the IF statement, and that this parameter should also state that the skeleton must be in combat to flip. Therefore it will interfere with the walking system.

Now the Enemy is facing the player when in combat.

Setting Up the Hitbox:

Back within Unity, we are first going to prep the skeleton with several components. Just like how we implement the hitbox with the player we will follow a very similar structure with our enemies.

First we will need a new sprite child component under the skeleton sprite object.

To see the component, we need to set the order in Layer to at least 50 to 51. Then use the “UI Sprite” as a Visual.

In that new sprite component, we will need to add a Box Collider 2D along with a Rigidbody 2D. Set the Collider to have “is Trigger” to true. Then in the Rigidbody 2D make sure that the Gravity Scale is set to zero.

Once that is all setup we can start animating the hitbox just like how we did with the player.

During the animation sequence I made sure that the box collider is turned off at the beginning and turned on during the flaming section. Learning from when I animated the Player, I only toggle the hitbox collider and animation during key important parts.

Next we are going to separate the Enemy and the Enemy Hitbox in its own layer to prevent the enemy hitbox from hitting itself during collision.

Setting the Skeleton hitbox as the “Enemy Attack Layer”
In the Player settings under Physics 2D, we are going to have the Enemy Attack ignore the Enemy.

Damage to Player:

The beauty of interface scripts is that with one general script, other scrips are able to inherit or get and set the interface script properties. This means that we don’t need to re-write another damage script just for the player, we can use the IDamageable interface script for the player. We can also use the same Attack script that we placed on the player for the Enemy Skeleton.

Player script using the IDamageable interface.

You might noticed that the minute you use the IDamageable, their are errors, this is because you need to resolve the Health and Damage methods that are required by the IDamageable Interface.

Now back in Unity we can test out if the IDamageable Interface is working against the player.



Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.