Journal 102 — Unity 2D Mobile, Dungeon Escape Skeleton Attack

Chris Nielsen
3 min readOct 4, 2021

Objective: To set up the skeleton attack behavior

In the previous journal, I went over the set up for the skeleton when he gets attacked by the player. In this article, I will go over the set up for when the skeleton gets close to the player and is attacked, so the skeleton attacks.

We can set up the animation behaviors where the skeleton should attack when the “InCombat” bool is true.

Let’s see the results.

We need to make sure the skeleton faces the player when attacking. Let’s first check the direction, where we can calculate a Vector3 and find the x-component.

If the x-component is positive, the player is to the right of the enemy, and if the x-component is negative, the player is the left of the enemy.

We can update the Enemy base class to flip the sprite if the player is to the left of the enemy.

Skeleton Hitbox

Now let’s create the skeleton hitbox like we did earlier for the player. We can add a 2D sprite to the skeleton sprite, with a Box Collider 2D and Rigidbody 2D.

Now we can remove the sprite renderer and just use the box collider to align with the first frame and then record each collider change with the weapon position.

Skeleton Hitbox Layer

Similar to the player, we want to make sure the enemy attack is not affected by the enemy’s own collider. So we need to create two layers, one for the enemy and one for the enemy attack. Then in the Physics 2D Project Settings, we want to have the enemy attack ignore the enemy.

Skeleton Attack Player

Now that the skeleton can attack, we need to update the player script so that the player also uses the interface IDamageable. The player needs hit points and the ability to be damaged by the enemies.

Let’s see the results.

Thank you for your time!

--

--

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.