In the previous lesson, we did some raycasting so that we could fire on the enemy soldier. In this lesson, we'll write a script to set a value for the enemy. Then, whenever the player shoots the enemy, we can deduct points from the health value. When the enemy's score reaches zero, we can play the soldier's dying animation. Let's get started.
In the Project area at the bottom of Unity, click on your Scripts folder. Right-click inside the Scripts folder. From the menu that appears, select Create > C# Script. When the script is created, type a new name for it. Call it EnemyHealth. Then double click the script to open it up in your coding editor.
You can delete the Update method (and any comments) and the Start method because we don't need them, at the moment. Now add these two variables inside the curly brackets of the class:
public int currentHealth = 20;
public static bool isEnemyDead = false;
The first one is an integer called currentHealth. We'll use this to keep track of the enemy's health. We've made it public because this script can be attached to any other enemy we add. The new enemies can then have their own values set. For example, you might want to set a higher value for tougher enemies. This higher value will then override the default value of 20 that we've set.
The second variable is a Boolean called isEnemyDead. It's set to false. We'll set it to true when the enemy bites the dust. But notice that it's public and static. That's because we'll need to access it soon from another script. (However, public static variables can be hard to keep track of and cause bugs in your code, so we'll change this in a later tutorial. It's OK for now, though.)
Now add this method to your code, just below the variables:
public void TakeDamage(int damage)
This is a method that we've called TakeDamage. We want to pass in an int value. The variable name we've come up with for this is damage. The method is void because we don't need to return a value. Notice that the method is public. That means it will be able to be seen from outside of this class, which is what we want as we're going to be calling it when the player shoots the enemy.
As the code for your TakeDamage method, add this between the curly brackets, then we'll run through it:
currentHealth = currentHealth - damage;
if (currentHealth <= 0 && isEnemyDead
Debug.Log("DEAD: " + currentHealth);
isEnemyDead = true;
The if statement checks two things: if the value inside of the currentHealth variable is less than or equal to zero, and if the Boolean variable isEnemyDead is false. If both of those things are true then the code for the if statement gets executed. The first line is just a debug line, so you can see the value of currentHealth. The next line is this:
We're going to attach this script to the Soldier. This will then be the gameObject. We then get the Animator component attached to the Soldier. This, remember, holds all our animations. We can then Play the animation called Dying. (The name of the animation you want to play needs to go between two double quotes.)
The final line sets isEnemyDead to true. We need to do this so the enemy won't fire after he's dead.
But the code in your editor should look like this:
Now go back to your GunFire script. Inside of your GunshotHitCheck method, add this line just below the Debug line for "Hit: " + damage: (The new line is in bold.)
Your GunshotHitCheck method should look like this:
The hit object (which would be the enemy) has a collider object. The collider allows you to get a component. In between the left and right pointy brackets of GetComponent, you can type the name of a script. In this case, we're typing the name of that EnemyHealth script we've just created. Type a pair of round brackets then a dot and you can select any public method or variable that's in the EnemyHealth script. We want to call that TakeDamage method. We hand it the value of damage, which we set up at the top of the GunFire script and is set to a value of 5.
Save your code and go back to Unity. The Enemy Health script is not attached to anything yet. To attach it to the Soldier, you can either drag and drop the script onto Soldier in the Hierarchy or drag and drop the script onto the Inspector with the Soldier selected (drag and drop above the Add Component button.) The image below shows both:
Notice the Current Health value in the Inspector on the right, in the image above. It has a default value of 20, which is the value we added when setting up the variable. You can change that to any positive number you like.
Now play your game. Shoot the Soldier a few times. You should see the animation play when you've got him. The short video below shows that, and also highlights a problem we need to solve.
The problem is that even though the soldier is dead, he's still trying to follow us!
To fix the issue, we can use the isEnemyDead Boolean in another script, which is why we set it up as public and static (we'll be changing this later). So, go back to your EnemyLookAt script. Change it from this:
if (EnemyHealth.isEnemyDead == false)
We've wrapped the single line of code we had in an if statement:
if (EnemyHealth.isEnemyDead == false)
To reference another script, you can use the name of the script. Previously, in GunFire, we used GetComponent<EnemyHealth>(). That's because we didn't have a simple static variable to access but a public method. This time, we just need to access the isEnemyDead variable from EnemyHealth. We only want the enemy to look at the player if isEnemyDead is false.
Save the script and go back to Unity. Play your game and you should find that the dead soldier doesn't try to follow us, as shown in the short video below:
OK, now that we can kill the enemy, let's write code so that the enemy can kill us.