Book Image

Unity AI Game Programming - Second Edition

By : Raymundo Barrera, Aung Sithu Kyaw, Clifford Peters, Thet Naing Swe
Book Image

Unity AI Game Programming - Second Edition

By: Raymundo Barrera, Aung Sithu Kyaw, Clifford Peters, Thet Naing Swe

Overview of this book

<p>Unity 5 provides game and app developers with a variety of tools to implement artificial intelligence. Leveraging these tools via Unity’s API or built-in features allows limitless possibilities when it comes to creating your game’s worlds and characters. Whether you are developing traditional, serious, educational, or any other kind of game, understanding how to apply artificial intelligence can take the fun-factor to the next level!</p> <p>This book helps you break down artificial intelligence into simple concepts to give the reader a fundamental understanding of the topic to build upon. Using a variety of examples, the book then takes those concepts and walks you through actual implementations designed to highlight key concepts, and features related to game AI in Unity 5. Along the way, several tips and tricks are included to make the development of your own AI easier and more efficient.</p> <p>Starting from covering the basic essential concepts to form a base for the later chapters in the book, you will learn to distinguish the state machine pattern along with implementing your own. This will be followed by learning how to implement a basic sensory system for your AI agent and coupling it with a finite state machine (FSM). Next you will be taught how to use Unity’s built-in NavMesh feature and implement your own A* pathfinding system. Then you will learn how to implement simple flocks and crowd’s dynamics, the key AI concepts. Then moving on you will learn how a behavior tree works and its implementation. Next you will learn adding layer of realism by combining fuzzy logic concepts with state machines. Lastly, you learn applying all the concepts in the book by combining them in a simple tank game.</p>
Table of Contents (15 chapters)
Unity AI Game Programming Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

In this book, we'll be exploring the world of artificial intelligence (AI) as it relates to game development. No matter what kind of game you are developing, you will surely find a myriad of uses for the content in this book—perhaps in ways that even I could not imagine.

The goal of this book is not to make you an expert, as it would take many, many, years and many more pages to do this, but to provide you with the knowledge and tools to embark on your own AI journey. This book covers the essentials, and by the end, you will have all that you need to implement AI in your own game, whether you choose to expand upon the examples provided or take the knowledge and do something new and exciting with it.

You will get the most out of this book and the examples provided by following along and tinkering with the code and project files provided. Each chapter will provide a conceptual background and some examples and will challenge readers to think of ways in which they can use these concepts in their games.

What this book covers

Chapter 1, The Basics of AI in Games, aims to demystify some of the most basic concepts of AI as it is a very vast and intimidating topic.

Chapter 2, Finite State Machines and You, covers one of the most widely used concepts in AI—the finite state machine.

Chapter 3, Implementing Sensors, covers some of the most important ways for a game AI agent to perceive the world around it. The realism of an AI agent is directly linked to how it responds to its environment.

Chapter 4, Finding Your Way, covers the most widely used pattern in pathfinding for game AI agents. The agents in games need to traverse the areas of the game levels and maneuver around obstacles along the way.

Chapter 5, Flocks and Crowds, covers flocking and crowd simulation algorithms, allowing you to handle the unison movements of the agents in your game rather than having to figure out the logic for each agent.

Chapter 6, Behavior Trees, covers the process of implementing a custom behavior tree as it is one of the most common ways to implement complex and compound AI behaviors in games.

Chapter 7, Using Fuzzy Logic to Make Your AI Seem Alive, shows you how to let the game AI agents make decisions based on various factors in a non-binary way. Fuzzy logic mimics the way humans make decisions.

Chapter 8, How It All Comes Together, covers an example of how various systems come together in a single-objective game template that can be easily expanded upon.

What you need for this book

To use the sample content provided with this book, you'll need a copy of Unity 5, which you can download for free from https://unity3d.com/get-unity. The system requirements for Unity can be found at https://unity3d.com/get-unity.

MonoDevelop, the IDE that comes bundled with Unity 5, is suggested but not required for this book as any text editor will do just fine. However, MonoDevelop comes with everything you need to write and debug code out of the box, including autocompletion, without the need for plugins or extensions.

Who this book is for

This book is intended for Unity developers with a basic understanding of C# and the Unity editor. Whether you're looking to build your first game or trying to expand your knowledge as a game programmer, you will find plenty of exciting information and examples of game AI in terms of concepts and implementation. This book does not require any prior technical knowledge of how game AI works.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We'll name it TankFsm."

A block of code is set as follows:

using UnityEngine;
using System.Collections;

public class TankPatrolState : StateMachineBehaviour {

   // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
   //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
   //
   //}

   // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
   //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
   //
   //}

   // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
   //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
   //
   //}

   // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
   //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
   //
   //}

   // OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
   //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
   //
   //}
}

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "When the panels are closed, you can still create new layers by clicking on the Layers dropdown and selecting Create New Layer."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit https://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/8272OT_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.