Home and Learn: Games Programming Course
So far, we have an enemy that can chase the player. And we can detect when the zombie reaches the player. But it's a bit static when it gets there. And there are no repercussions when the player takes a hit.
What we'll do in this lesson is to play an attack animation when the zombie enemy gets close. And we'll add a health script to the player.
Go back to your Animator screen. If you have closed it down, click Window from the menus at the top of Unity. From the Window menu, select Animation > Animator. Your Animator screen should look like this, so far:
We can add another animations here. So go back to your Zombie animations folder, which was this one, if you remember:
We like the animation called Z_Attack. Drag this one into your Animator window:
We don't need to do anything else here, as we'll play the animation through some code.
So, go back to your Hierarchy. Select your zombie. In the Inspector on the right, click the Add Component button. Add a new script. Call it ZombieHitPlayer. You should have this for your zombie:
Double click the name of your script above to open it up in your code editor.
When your code editor opens up, delete all the default code. Replace it with this:
using UnityEngine;
public class ZombieHitPlayer : MonoBehaviour
{
private Animator enemy_anim;
private void Start()
{
enemy_anim = GetComponent<Animator>();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
enemy_anim.Play("Z_Attack");
Debug.Log("Zombie hit player");
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
enemy_anim.Play("Z_Walk1");
Debug.Log("Zombie leaving player");
}
}
}
It should look like this in your Editor:
What the code does is to get the Animator attached to the zombie. We then use the in-built method OnCollisionEnter. In between the round brackets of OnCollisionEnter, there is a variable called collision. We can use this to find out what the zombie is colliding with. We do this with CompareTag. If the game object the zombie is colliding with has the tag Player, then we can play that Z_Attack animation we added to the Animator window. We can use OnCollisionExit to set our animation back to the walking animation, Walk_1.
The other lines are Debug ones. We can use the Console window to do some checking. You can comment out any line by typing two forward slashes in front of it. The code then gets ignored:
// Debug.Log("Zombie hit player");
But save your code and go back to Unity. In the Hierarchy on the left, select your Player item:
Now, in the Inspector on the right, set the Tag at the top to Player:
There should already be a Player tag set up. Simply select it from the dropdown list:
(If you can't see a Player tag, you'll have to add your own, just like we did before. Click the Add Tag item at the bottom of the menu and set one up)
With the Player tag added, play your game. The zombie should fly into a rage when he's close to you and then calm down a bit when you move away. As in the 10 second video below:
Before playing your game, you can show the Console. If you can't see
a Console window, click Window from the menus at the top of Unity.
From the Window menu, select General > Console. (The shortcut
for the console is CTRL + SHIFT + C)
What we need now is to set up a health score for the player. When the player gets hit by the zombie enemy, we can deduct health points from the player.
With your Player still selected in the Hierarchy, click the Add Component button in the Inspector on the right. Add a New Script. Call it PlayerHealth:
Double click the name of your script in the box above. This will open it up in your code editor. Delete the entire default script. Now copy and paste the following:
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
private int currentHealth;
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
Debug.Log("Player Health: " + currentHealth);
if (currentHealth <= 0)
{
Die();
}
else {
Debug.Log("HEALTH= " + currentHealth);
}
}
private void Die()
{
Debug.Log("Player has died!");
// Add stuff here: respawn player, Game Over Screen, etc
}
}
It should look like this in your editor:
The script is mainly just a public method called TakeDamge. In between the round brackets of TakeDamage, there is an int variable called damage. We're going to add a few lines to our zombie attack script. There, we can pass in a value for damage, which will be how many points we want to deduct from the player's health.
The rest of the script first sets a value for the player's maxHealth. We've set it to 100. Notice that it is a public variable. This means it will show up in the Inspector and you can change the value form there, if you want a higher or lower value for maxHealth.
Once we have a value for maxHealth, the script deducts health points from the Player, if Player takes a hit. There are lots of debug lines so we can see what's happening in the Console window.
Notice we have a method called Die. This gets activated when the Player's current health is less than zero. You could do things like play a animation clip for the player dying, do a respawn, display a Game Over screen, etc.
Save your script and go back to Unity. You should see that maxHealth variable appear under the script name:
You can delete the 100 and type a new value here. The new value will be used in the script.
Now we need to amend our ZombieHitPlayer script. This script is attached to the zombie, remember. So open the script back up. We only need a short if statement. Add the lines below, add them just under line 16, the Debug.Log("Zombie hit player"); line.
if (collision.gameObject.TryGetComponent<PlayerHealth>(out
var playerHealth))
{
playerHealth.TakeDamage(10);
}
Here's what your code should look like. The new lines are highlighted in pink, lines 18 to 21:
What the script does is to Instantiate that PlayerHealth script we wrote. It creates a variable called playerHealth. The playerHealth variable is then an object that we can use to call our TakeDamage method. We're passing in a value of 10, meaning 10 health points will be deducted from the player on each hit. You can change the 10 to anything you like.
But save the code and go back to Unity.
Play your game again. Display your console. Let the zombie attack you and you should see this appear:
Of course, instead of displaying the player's health in a console window, you'll want to display a health value on screen. We'll do that next.
<--Back to the Unity 3D Course Contents Page
Email us: enquiry at homeandlearn.co.uk