Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity 2017 Game AI Programming - Third Edition - Third Edition

You're reading from  Unity 2017 Game AI Programming - Third Edition - Third Edition

Product type Book
Published in Jan 2018
Publisher Packt
ISBN-13 9781788477901
Pages 254 pages
Edition 3rd Edition
Languages

Table of Contents (10) Chapters

Preface 1. The Basics of AI in Games 2. Finite State Machines and You 3. Implementing Sensors 4. Finding Your Way 5. Flocks and Crowds 6. Behavior Trees 7. Using Fuzzy Logic to Make Your AI Seem Alive 8. How It All Comes Together 9. Other Books You May Enjoy

Finite State Machines and You

In this chapter, we'll expand our knowledge of the FSM pattern and its uses in games and learn how to implement it in a simple Unity game. We will create a tank game with the sample code that comes with this book. We'll be dissecting the code and the components in this project. The topics we'll cover are as follows:

  • Understanding Unity's state machine features
  • Creating our own states and transitions
  • Creating a sample scene using examples

Unity 5 introduced state machine behaviors, which are a generic expansion of the Mecanim animation states that were introduced in the 4.x cycle. These new state machine behaviors, however, are independent of the animation system, and we will learn to leverage these new features to quickly implement a state-based AI system.

In our game, the player will be able to control a tank. The enemy tanks...

Technical Requirements

Finding uses for FSMs

Although we will primarily focus on using FSMs to implement AI in our game to make it more fun and interesting, it is important to point out that FSMs are widely used throughout game and software design and programming. In fact, the system in Unity 2017 that we'll be using was first introduced in the Mecanim animation system.

We can categorize many things into states in our daily lives. The most effective patterns in programming are those that mimic the simplicity of real-life designs, and FSMs are no different. Take a look around and you'll most likely notice a number of things in one of any number of possible states. For example, is there a light bulb nearby? A light bulb can be in one of two states—on or off (so long as we're not talking about one of those fancy dimming lights). Let's go back to grade school for a moment and...

Creating state machine behaviors

Now that we're familiar with the concept of a state machine, let's get our hands dirty and start implementing our very own.

As of Unity 2017.1, state machines are still part of the animation system, but worry not, they are flexible and no animations are actually required to implement them. Don't be alarmed or confused if you see code referencing the Animator component or the AnimationController asset as it's merely a quirk of the current implementation. It's possible that Unity will address this in a later version, but the concepts will likely not change.

Let's fire up Unity, create a new project, and get to it.

Creating the AnimationController asset

The AnimationController...

Setting up our player tank

Open up the sample project included with this book for this chapter.

It is a good idea to group similar assets together in your project folder to keep them organized. For example, you can group your state machines in a folder called StateMachines. The assets provided for this chapter are grouped for you already, so you can drop the assets and scripts you create during this chapter into the corresponding folder.

Creating the enemy tank

Let's go ahead and create an animator controller in your assets folder. This will be your enemy tank's state machine. Call it EnemyFsm.

This state machine will drive the tank's basic actions. As described earlier in our example, the enemy can patrol, chase, and shoot the player. Let's go ahead and set up our state machine. Select the EnemyFsm asset and open up the Animator window.

Now, we'll go ahead and create three empty states that will conceptually and functionally represent our enemy tank's states. Name them Patrol, Chase, and Shoot. Once they are created and named, we'll want to make sure we have the correct default state assigned. At the moment, this will vary depending on the order in which you created and named the states, but we want the Patrol state to be the default state, so right-click on it and select Set...

Summary

In this chapter, we learned how to implement state machines in Unity 2017 using animator and controller-based state machines for what will be our tank game. We learned about state machine behaviors and transitions between states. With all of these concepts covered, we then applied the simple state machine to an agent, thus creating our first artificially intelligent entity!

In the next chapter, we'll continue to build our tank game and give our agent more complex methods for sensing the world around it.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Unity 2017 Game AI Programming - Third Edition - Third Edition
Published in: Jan 2018 Publisher: Packt ISBN-13: 9781788477901
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}