Building in Enemy hit Behaviors

Jordan Evans
Oct 11 · 5 min read

Now that we have a way for our targets to know that they are being hit, we need to look at how we can build further upon that and have our enemies react to being hit.
To start, what we are going to do is create a health system for the enemy units so that we can kill them. First off, let’s see how we would go about building it into our script:

What we have done is created a simple health decrease method within our skele unit. When we test this in the game however, we will be met with a slight issue:

What is happening is that our swing is managing to hit the target multiple times within the same swing. In order to fix this, we have to figure out a way to allow a cooldown in hit detection so that if we hit something, it won’t be hit again after a certain period of time:

When we are dealing with a time based system upon interactions between things, the best type of method to use would be a IEnumerator. In this case, we create a 0.5 second grace period between time periods that the skeleton can get hit. We have to make sure that our Damage() method is able to be called upon when our can damage bool is true:

Now that we have it set up so that we need 3 swings to properly deal with an enemy that has 3hp, lets get ourselves a hit animation going. We will add in our animation like how we have done many times before, and in the script under our skele enemy, we are going to add 1 line of code:

From here, we can test it within our game:

As we can see, the animation for getting hit will play, however there are a few issues at hand. First, our skele keeps walking while it is hit, then it freezes in it’s idle state as we made it transition to idle after being hit before moving a few seconds later. To fix this, we are going to create logic for our skeleton to enter a combat mode and remain idle until our player moves far enough away from the enemy. To start, we are just going to have it loop it’s idle animation:

What we have done is in our script have our new bool of InCombat be set to true once our player hits the enemy. As we can see, it keeps our enemy in an idle position, but now we need to look at how we can adjust it so that the enemy will go back to moving around once the player is far enough away.

To start, we will need to create a method in which we can measure the distance between our objects:

From here, we can test to see if it properly records our distance between the skeleton and the player. However, in order for us to do this, we will want to go into our skele script and make a quick addition for the tracking there so that we don’t get our console filled up with the distance of all the enemies on the field:

Now that we can see that it is being tracked, we will want to make a quick adjustment in our void update on the enemy so that we are able to turn the tracking back on after the player has hit the enemy:

We have this in place because what would happen if we didn’t add the GetBool is our movement method would never be called, thus the distance tracking will never happen. Once we get all of this working, let’s see how it looks within our game:

Next, let’s get to work on having our skeleton swing back at our player. To start, we are going to have it look towards the player when it swings:

What this code allows us to do is flip the skeleton based on if the distance between it and the player is positive or negative on the x axis. We also only want this to trigger when the skeleton is in combat, so we have to specify that as well:

Now that we have our skeleton swinging at the player, we just need to create a hitbox on it, just like how we did with our players swing:

Finally, let’s get our player set up so that it has the IDamageable interface within it, as that particular script is not set up with just 1 object in mind:

With this set up, we can check within our game to make sure it is getting called when the skeleton swings at us:

Now that we have our skeleton enemy hitting our player, we can look to apply this logic to our other enemy types and continue on with the next parts of our game.

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Jordan Evans

Written by

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.