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.
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.
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.
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.
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."
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 www.packtpub.com/authors.
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.
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.
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.
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 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.