Working with 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.

Animation states can be referred to as the character's state of animation. For example, walking is a state, as are running and jumping.

Here are the states that the beetle can have, each tied to an animation:

  • Idle on Ground
  • Walking on Ground
  • Eating on Ground
  • Attacking on Ground
  • Die on Ground
  • Stand
  • Standing Idle
  • Standing Walk
  • Standing Run
  • Standing Attack
  • Die Standing

With the preceding list of states, we can assign the following transitions:

  • From Idle on Ground to:
    • Walking on Ground
    • Running on Ground
    • Eating on Ground
    • Attacking on Ground
    • Stand
  • From Stand to:
    • Standing Idle
    • Standing Walk
    • Standing Run
    • Standing Attack

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:

  1. Left-click the state button
  2. Look in the Inspector panel to determine which animation is associated with the state button
  3. Rename the state name in the Inspector panel to reflect the animation.
  4. Click the return button
  5. Double-check the state button to ensure your change was made

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:

  1. Right-click the originating state.
  2. Select Create Transition.
  3. Click on the destination state.

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:

  1. Select the Beetle character in the Hierarchy panel. 
  2. In the Inspector panel, click the Add Component button.
  3. Select Physics | Box Collider.
  4. Click the Edit Collider button.

 

  1. Modify the size and position of the box collider so that it encases the entire beetle body.
  2. Click the Edit Collider button again to get out of edit mode.

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:

  1. Select the Beetle character in the Hierarchy panel. 
  2. In the Inspector panel, click the Add Component button.
  3. Select New Script.
  4. Name the script BeetleNPC.
  5. Click the Create and Add button.
  6. In the project view, select Favorites | All Scripts | BeetleNPC.
  7. Double-click the BeetleNPC script file.
  8. Edit the script so that it matches the following code block:
 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:

  • Earlier in this section, you renamed all the states in the Animation Controller window. The names you gave the states are the ones you will reference in code. 
  • Since the animation we used did not have any transitions to other states, the Cucumber Beetle will remain in the final position of the animation unless we script it otherwise. So, if we had 100 beetles and defeated them all, all 100 would remain on their backs in the game world.

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. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset