Home and Learn: Games Programming Course
We're going to have a zombie stumbling around the graveyard we created in previous lessons. Again, we'll download a free asset for this.
As before, from the menus at the top of Unity, select Window > Asset Store. Now copy and paste this link in your browser:
https://assetstore.unity.com/packages/3d/characters/humanoids/zombie-30232
This free game asset is by Pxltiger and is a 5-star asset - it's very popular zombie. Not only do you get the model for free, but you also get some animations thrown in.
So download the asset and it should open in the Unity Package Manager for you to import into your project:
Once it's installed, take a look at the Projects area at the bottom of Unity. You should see a Zombie folder inside of your Assets folder:
Again, as in previous lessons, you may need to convert the materials. Move inside of the Materials folder. If you see any Magenta circles, select them all. From the menus at the top of Unity, select Edit > Rendering > Materials > Convert Selected Built-In Materials to URP.
Once the materials have been converted, right click inside of the Materials folder. From the menu, select Reimport All. Unity will restart. Once it's reloaded, move into the Prefabs folder to see the zombies:
Drag one of the zombies from the Prefabs folder into your scene. You may need to change the Scale and the Rotation Y in the Inspector. If you hit Play now, you'd see that the zombie cycles through just about all of the animations that have been set up:
Not quite what we want!
To fix it, we can create our own Animation Controller and add just the animations we need.
In the main Zombie folder, create a new folder for yourself. Call it Animators. Right click inside of your new Animators folder. From the menu that appears, select Create > Animation > Animator Controller:
Rename the controller to AC_ZOMBIE_1:
Now double-click the file to open it up. You should see the Animator window appear:
In the Project folders at the bottom of Unity, move inside of the zombie animations folder:
The green triangles are the animations. The icons with the zombies are the full models. The green square called Zombie is an Animation Controller. If you click on it, you'll see all the animations linked together, which is why the zombie from the video cycled through so many animations:
We don't want this controller, as we've just created our own.
Go back and select your own Animator Controller. Move back inside of the animation folder. Select the Animation called Z_Walk (the green triangle). Notice the Inspector on the right, it will have a default model. Press the Play button to see the default model with the animation applied:
You can drag your zombie from the Unity Hierarchy onto this window. The default model will be replaced with your zombie:
Click on a different green triangle and you'll be able to see what each animation looks like.
Now drag the green animation called Z_Walk1 into the Animator window. You should see this:
The reason there is a line running from the Entry block to the Z_Walk1 block is that this animation is now the default state - it will play on the zombie when the game starts.
We'd like to add a dying animation to the animator, as well. So drag the green Z_FallingBack triangle from the Animations folder into the Animator. You should see this:
If you wanted to, you could make this Z_FallingBack animation the default one. Just right click the block. From the menu that appears, select Set as Layer Default State:
The line from the Entry block would then connect to the Z_FallingBack animation. When the game starts, it would this animation that would play:
But make sure the Z_Walk1 is the default layer state.
You can rename these animation states. We can rename Z_FallingBack to Die. That way, it will be easier to remember when we come to play this animation through some short code we'll write.
So, with the Z_FallingBack block selected in the Animator window, have a look at the Inspector on the right. Change the name at the top to Die:
Note: Changing the name here just changes the name of the block. It doesn't change the name of the animation green triangle. That will still be called Z_FallingBack, which is the motion we want.
Now we need to change the name of the Animator that our zombie uses. So, select your zombie in the Hierarchy. In the Inspector on the right, change the Controller to the one you have just created - AC_ZOMBIE_1:
Play your game again, you should see your zombie walking forward, as in the short video below:
Now let's shoot the zombie.
If you look closely, you'll see that your gun actually shoots a bullet. Play your game and you should see a speck of a bullet flying away. What we'll do is to set up a collider on the zombie. We'll then be able to detect if the bullet hits the collider. If it does, we can play our Die animation.
First, we'll need a Tag for the bullet. You'll see why soon. So, locate the bullet prefabs in the Nokobot Handgun folders. The folder is here:
Assets/Nokobot/Modern Guns - Handgun/_Prefabs
Click on the bullet to select it:
In the Inspector on the right, locate the Tag area:
Click the dropdown to see what tags are already set up. We need to add a new tag, so select the Add Tag option:
Click the Plus button to add a new tag:
Type a name for your tag. Call it Bullet. Then, either click the Save button, or just press the Enter key on your keyboard:
You should see your new tag on the list:
Click back on the Bullet prefab in the _Prefabs folder. In the Inspector on the right, select your new Bullet tag from the list:
Now that the bullet is tagged, we can set up the zombie to get hit by a bullet.
In the Hierarchy on the left, select your zombie. In the inspector on the right, click the Add Component button. Add a Box Collider:
If you look at the zombie in the scene, you'll see the green outline of the box collider:
To move and resize it, you use the Center and Size option in the Inspector. The Y Center moves the box up and down; use the X, Y, and Z values on the Size option to change the size of the box collider:
Here's what ours look like in the scene:
We've repositioned and resized the box collider so that it only covers its head. That's because, well, it's a zombie - we need headshots only to kill it!
If you are having trouble resizing your box collider, you can use the Orientation arrows in the top right of the scene. Click the Green Y to see a top-down view:
Click the Blue Z to see a side view
Click the Red X to see a front view:
One last thing to do is for the box collider is to check Is Trigger:
The reason to check the box is that there is an inbuilt C# method in Unity called OnTriggerEnter. This will come in very handy!
Right click your main Assets folder in the Projects area of Unity. From them menu that appears, select Create > Folder. Call the folder Scripts. Move inside of this Scripts folder and right click. From the menu that appears, select Create > MonoBehavior Script. (MonoBehavior Script is also on the Scripting menu when you right click.) A new C# script will be created. Call the script KillZombie:
Double click the script and it will open in your script editor. Now, delete the entire contents of the script. Copy and paste the following code:
using UnityEngine;
public class KillZombie : MonoBehaviour
{
private Animator enemy_anim;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Bullet")) {
enemy_anim = GetComponent<Animator>();
enemy_anim.Play("Die");
Destroy(gameObject, 2.5f);
}
}
}
It should look like this in your coding editor:
Save the script and go back to Unity. Now drag and drop the script either into the Inspector on the right, or onto the zombie in the Hierarchy:
(Another way to add the script is to click the Add Component button and type the name of your script in the search box.)
Before going through the code, test your game out. Shoot the zombie and you should find that he dies then disappears from the scene, as in the video below:
One thing's for sure, though - we need a sight on the gun so we can see where we're aiming! We'll do that in the next lesson. First, for those interested, here's how the script works.
The script first sets up a variable called zombieAnimator. The type of variable is an Animator:
private Animator zombieAnimator;
Next, we have a method called OnTriggerEnter. This is a built-in method that can be used to detect when something entered a trigger. (We checked Is Trigger when we set up our Box Collider.) In between the round brackets of OnTriggerEnter there is a variable called other. This is of type collider.
There is only an if statement in our OnTriggerEnter method:
if (other.CompareTag("Bullet")) { }
We're checking if the other thing that entered the trigger has a tag called Bullet. We're using the CompareTag method for this.
If the other thing is indeed a bullet, then we get the Animator component and store it in our variable:
zombieAnimator = GetComponent<Animator>();
We then play our Die animation:
zombieAnimator.Play("Die");
We changed the name of the animation from Z_FallingBack to Die. We could have left the name of the animation on Z_ FallingBack. In which case, the line of code would be this:
zombieAnimator.Play("Z_ FallingBack");
You can play any animation just by using its name between quote marks.
Finally, we destroy the game object that this script is attached to, which is the zombie:
Destroy(gameObject, 2.5f);
The 2.5f means 2.5 seconds. You can change this to anything you like.
In the next lesson, we'll add that gun sight so that we can actually aim our gun.
<--Back to the Unity 3D Course Contents Page
Email us: enquiry at homeandlearn.co.uk