Book Image

Practical Game AI Programming

By : Micael DaGraça
Book Image

Practical Game AI Programming

By: Micael DaGraça

Overview of this book

The book starts with the basics examples of AI for different game genres and directly jumps into defining the probabilities and possibilities of the AI character to determine character movement. Next, you’ll learn how AI characters should behave within the environment created. Moving on, you’ll explore how to work with animations. You’ll also plan and create pruning strategies, and create Theta algorithms to find short and realistic looking game paths. Next, you’ll learn how the AI should behave when there is a lot of characters in the same scene. You'll explore which methods and algorithms, such as possibility maps, Forward Chaining Plan, Rete Algorithm, Pruning Strategies, Wall Distances, and Map Preprocess Implementation should be used on different occasions. You’ll discover how to overcome some limitations, and how to deliver a better experience to the player. By the end of the book, you think differently about AI.
Table of Contents (17 chapters)
Title Page
About the Author
About the Reviewer
Customer Feedback
Navigation Behavior and Pathfinding
AI Planning and Collision Avoidance


Developing games is a passion for some, and I believe that this is because we are able to create a world that is completely imagined by us; this is like being a god, and the AI characters that we place there are the habitants of that world that we have just created. We are free to imagine how they will behave, we can create a society according to our imagination, we are able to create a sweet and kind character, but also we can create the most devilish character that ever existed--the possibilities are endless, and that is why we will always have new game ideas coming out. No matter what game genre we decide to develop, the world and their characters will be the essence of our vision; this is what will make our game unique, and ideally we should be able to create everything that we have in mind, just as we imagine. This book was conceived with that in mind, that all of us should be able to create the ideas that we have and that we shouldn't limit our imagination, so this book will cover the foundations of creating an artificial character, and after reading it, we should be able to explore all the topics that you have learned, creating AI characters that fit perfectly with what we have imagined.

What this book covers

Chapter 1, Different Problems Require Different Solutions, is a brief introduction to the video game industry and game AI.

Chapter2, Possibility and Probability Maps, focuses on how to create and use probability and possibility maps for AI characters.

Chapter 3,Production Systems, describes how to create a set of rules necessary for the character AI to achieve their goals.

Chapter 4, Environment and AI, focuses on the interaction between the characters in the game and their environment.

Chapter 5, Animation Behaviors, shows best practices to implement animations in our game.

Chapter 6, Navigation Behavior and Pathfinding, focuses on how to calculate the best options for the AI to move in real time.

Chapter 7, Advanced Pathfinding, focuses on the use of theta algorithms to find short and realistic-looking paths.

Chapter 8, Crowd Interactions, focuses on how the AI should behave when there are a lot of characters on the same scene.

Chapter 9, AI Planning and Collision Avoidance, discusses the anticipation of the AI, knowing in advance what they will do when arriving at a position or facing a problem.

Chapter 10, Awareness, focuses on working with awareness systems to create stealth genre mechanics.

What you need for this book

It is recommended that you have install a game engine that uses C# (Unity3D has a free version, and it was used for the examples covered on the book).

Who this book is for

This book is intended for developers who have already created their first games in C# and seek to expand their capabilities with AI, creating crowds, enemies, or allies that can behave autonomously.


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: "The variables that we'll be using for now are Health, statePassive, stateAggressive, and stateDefensive."

A block of code is set as follows:

if (playerPosition == "triggerM") 
 transform.LookAt(playerSoldier); // Face the direction of the player 
 transform.position = Vector3.MoveTowards(transform.position, 
 buildingPosition.position, walkBack); 

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: "In Unity we click under the Layers button to expand more options, and then we click where it says Edit Layers...."


Warnings or important notes appear in a box like this.


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 [email protected], 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

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 for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you. You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at We also have other code bundles from our rich catalog of books and videos available at Check them out!

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


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, 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 and enter the name of the book in the search field. The required information will appear under the Errata section.


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 [email protected] with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.


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