Journal 101 — Unity 2D Mobile, Dungeon Escape Skeleton Setup
Objective: To set up the skeleton damage system
In this article, after setting up the player attack hit box collider in the previous article, we want to now add the enemy animations and behavior for when it is attacked.
We can start with setting up the animation when the skeleton is hit by the player.
We can make a transition from “Any State” to the hit animation, and use a new trigger called “Hit”.
To start, we can define a new C# script called “IDamageable”. If other classes use the interface, they must have a health get and set property, and they must have a Damage() method. So as you can see when we try to add the IDamageable interface to the Moss Giant enemy, there is an error.
We need to add the health variable and Damage method to meet the requirements of the interface.
Now we can test the Damage() method to confirm if the player hits the skeleton. In the Attack() method, we want to check if the sword collision is with a game object that has an IDamageable interface (e.g., skeleton, moss giant, spider, box, destructible).
We can see if this works.
Skeleton Damage Implement
Now let’s update the skeleton functionality so it can take damage and be destroyed.
When swinging the sword, the hit box may trigger multiple hits on the enemy. We can make a cooldown to only allow damage from one collision at a time.
Skeleton Damage Add Hit Animation
Now let’s add the skeleton hit animation to the behavior. We already have a reference to the Animator in the Enemy base class. We also need to pause the enemy movement between waypoints while the hit animation plays with a bool variable in the Enemy base class. We can set up a “InCombat” bool in the Animator to have the enemy switch to idle.
Before we complete this section, we need to check the distance in the Enemy base class between the enemy and the player, and keep the enemy in combat if the player is close, such as 2 units away, and if the player gets farther away, switch the enemy back to walk state.
Here are the results.
In the next article, I will go over how to create the skeleton attack so it will fight back.
Thank you for your time!