Today, we will learn to create game characters while focusing mainly on non-player characters. Our Cucumber Beetles will serve as our game's non-player characters and will be the Cucumber Man's enemies. We will incorporate Cucumber Beetles in our game through direct placement. We will review the beetles' 11 animations and make changes to the non-player character's animation controller. In addition, we will write scripts to control the non-player characters. We will also add cucumber patches, cucumbers, and cherries to our game world.
Non-player characters commonly referred to as NPCs, are simply game characters that are not controlled by a human player. These characters are controlled through scripts, and their behaviors are usually responsive to in-game conditions.
Our game's non-player characters are the Cucumber Beetles. These beetles, as depicted in the following screenshot, have six legs that they can walk on; under special circumstances, they can also walk on their hind legs:
Cucumber Beetles are real insects and are a threat to cucumbers. They cannot really walk on their hind legs, but they can in our game.
You are now ready to import the asset package for our game's non-player character, the Cucumber Beetle. Go through the following steps to import the package:
As you will notice, the Cucumber_Beetle asset package contains several assets related to the Cucumber Beetles, including a controller, scripts, a prefab, animations, and other assets:
Now that the Cucumber_Beetle asset package has been imported into our game project, we should save our project. Use the File | Save Project menu option.
Next, let's review what was imported.
In the Project panel, under Assets | Prefabs, you will see a new Beetle.Prefab. Also in the Project panel, under Assets, you will see a Beetle folder. It is important that you understand what each component in the folder is for. Please refer to the following screenshot for an overview of the assets that you will be using in this chapter in regards to the Cucumber Beetle:
The other assets in the previous screenshot that were not called out include a readme.txt file, the texture and materials for the Cucumber Beetle, and the source files. We will review the Cucumber Beetle's animations in the next section.
Several Cucumber Beetle animations have been prepared for use in our game. Here is a list of the animation names as they appear in our project, along with brief descriptions of how we will incorporate the animation into our game. The animations are listed in alphabetical order by name:
Animation Name | Usage Details |
Attack_Ground | The beetle attacks the Cucumber Man's feet from the ground |
Attack_Standing | The beetle attacks the Cucumber Man from a standing position |
Die_Ground | The beetle dies from the starting position of on the ground |
Die_Standing | The beetle dies from the starting position of standing on its hind legs |
Eat_Ground | The beetle eats cucumbers while on the ground |
Idle_Ground | The beetle is not eating, walking, fighting, or standing |
Idle_Standing | The beetle is standing, but not walking, running, or attacking |
Run_Standing | The beetle runs on its hind legs |
Stand | The beetle goes from an on-the-ground position to standing (it stands up) |
Walk_Ground | The beetle walks using its six legs |
Walk_Standing | The beetle walks on its hind legs |
You can preview these animations by clicking on an animation file, such as Eat_Ground.fbx, in the Project panel. Then, in the Inspector panel, click the play button to watch the animation.
There are 11 animations for our Cucumber Beetle, and we will use scripting, later to determine when an animation is played.
In the next section, we will add the Cucumber Beetle to our game.
First, let's simply drag the Beetle.Prefab from the Assets/Prefab folder in the Project panel to our game in Scene view. Place the beetle somewhere in front of the Cucumber Man so that the beetle can be seen as soon as you put the game into game mode.
A suggested placement is illustrated in the following screenshot:
When you put the game into game mode, you will notice that the beetle cycles through its animations. If you double-click the Beetle.controller in the Assets | Beetle folder in the Project panel, you will see, as shown in the following screenshot, that we currently have several animations set to play successively and repeatedly:
This initial setup is intended to give you a first, quick way of previewing the various animations. In the next section, we will modify the animation controller.
We will use an Animation Controller to organize our NPCs' animations. The Animation Controller will also be used to manage the transitions between animations.
Before we start making changes to our Animation Controller, we need to identify what states our beetle has and then determine what transitions each state can have in relation to other states.
Here are the states that the beetle can have, each tied to an animation:
With the preceding list of states, we can assign the following transitions:
Reviewing the transitions from Idle on Ground to Stand demonstrates the type of state-to-state transition decisions you need to make for your game.
Let's turn our attention back to the Animation Controller window. You will notice that there are two tabs in the left panel of that window: Layers and Parameters. The Layers tab shows a Base Layer. While we can create additional layers, we do not need to do this for our game. The Parameters tab is empty, and that is fine. We will make our changes using the Layout area of the Animation Controller window. That is the area with the grid background.
Let's start by making the following changes. For all 11 New State buttons, do the following:
When you have completed the preceding five steps for all 11 states, your Animation Controller window should match the following screenshot:
If you were to put the game into game mode, you would see that nothing has changed. We only changed the state names so they made more sense to us. So, we have some more work to do with the Animation Controller.
Currently, the Attacking on Ground state is the default. That is not what we want. It makes more sense to have the Idle on Ground state to be our default. To make that change, right-click the Idle on Ground state and select Set as Layer Default State:
Next, we need to make a series of changes to the state transitions. There are a lot of states and there will be a lot of transitions. In order to make things easier, we will start by deleting all the default transitions. To accomplish this, left-click each white line with an arrow and press your keyboard's Delete key. Do not delete the orange line that goes from Entry to Idle on Ground.
After all transitions have been deleted, you can drag your states around so you have more working room. You might temporarily reorganize them in a manner similar to what is shown in the following screenshot:
Our next task is to create all of our state transitions. Follow these steps for each state transition you want to add:
Once you have made all your transitions, you can reorganize your states to declutter the Animation Controller's layout area. A suggested final organization is provided in the following screenshot:
As you can see in our final arrangement, we have 11 states and over two dozen transitions. You will also note that the Die on Ground and Die Standing states do not have any transitions. In order for us to use these animations in our game, they must be placed into an Animation Controller.
Let's run a quick experiment:
Your box collider should look similar to what is depicted in the following screenshot:
Next, let's create a script that invokes the Die on Ground animation when the Cucumber Man character collides with the beetle. This will simulate the Cucumber Man stepping on the beetle. Follow these steps:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BeetleNPC : MonoBehaviour {
Animator animator;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
}
// Collision Detection Test
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("Player"))
{
animator.Play("Die on Ground");
}
}
}
This code detects a collision between the Cucumber Man and the beetle. If a collision is detected, the Die on Ground animation is played. As you can see in the following screenshot, the Cucumber Man defeated the Cucumber Beetle:
This short test demonstrated two important things that will help us further develop this game:
This was a simple and successful scripting test for our Cucumber Beetle. We will need to write several more scripts to manage the beetles in our game. First, there are some game world modifications we will make.
To summarize, we discussed how to create interesting character animations and bring them to life using the Unity 2018 platform.
You read an extract from the book Getting Started with Unity 2018 written by Dr. Edward Lavieri. This book gives you a practical understanding of how to get started with Unity 2018.
Unity 2D & 3D game kits simplify Unity game development for beginners
Build a Virtual Reality Solar System in Unity for Google Cardboard
Unity plugins for augmented reality application development