This book is meant to help you to incorporate various Artificial Intelligence techniques into your games. We will discuss decision techniques such as Finite State Machines and Behavior Trees. We will also look at movement, obstacle avoidance, and flocking. We also show how to follow a path, how to create a path using the A* pathfinding algorithm, and then how to reach a destination using a navigation mesh. As a bonus we will go into detail about random and probability, and then incorporate these ideas into a final project.
Chapter 1, Introduction to AI, talks about what Artificial Intelligence is, and how it is used in games. Also, we talk about various techniques used to implement AI into games.
Chapter 2, Finite State Machines, discusses a way of simplifying how we manage the decisions, which AI needs to make. We use FSMs to determine how AI behaves in a particular state and how it transitions to other states.
Chapter 3, Random and Probability, discusses the basics behind probability, and how to change the probability of a particular outcome. Then we look at how to add randomness to our game to make the AI less predictable.
Chapter 4, Implementing Sensors, looks at where we should make our character aware of the world around them. With the ability of our characters to see and hear, they will know when an enemy is nearby and will know when to attack.
Chapter 5, Flocking, discusses a situation where many objects travel together as a group. We will look at two different ways to implement flocking, and how it can be used to make objects move together.
Chapter 6, Path Following and Steering Behaviors, looks at how AI characters can follow a path provided to reach a destination. Then we look at how AI characters can find a target without knowing a path, and by moving towards a goal while avoiding.
Chapter 7, A* Pathfinding, discusses a popular algorithm, which is used to find the best route from a given location to a target location. With A*, we scan the terrain and find the best path that leads us to the goal.
Chapter 8, Navigation Mesh, discusses using the power of Unity to make pathfinding easier to implement. By creating a Navigation Mesh (this requires Unity Pro), we will be able to represent the scene around us better then we could using tiles and the A* algorithm.
Chapter 9, Behavior Trees, expands upon Finite State Machines into something we can use for even the most complex of games. We will be using the free plugin Behave to help us create and manage Behavior Trees in Unity.
Chapter 10, Putting It All Together, takes various elements of what we have learned throughout the book and putting together one last project. From here you will be able to apply the remaining AI elements we learned and create an impressive vehicle battle game.
The main requirement for this book is having Unity Version 3.5 or higher installed. Chapter 8, Navigation Mesh talks about creating a Navigation Mesh, something that requires Unity Pro. In Chapter 9, Behavior Trees we download Behave, a free Behavior Tree plugin, which requires an account with the Unity Store. Both of these requirements are optional because the assets that come with this book already have the Navigation Mesh generated and the Behave plugin.
This book is for anyone who wants to learn about incorporating AI into games. This book is intended for users with prior experience of using Unity. We will be coding in C#, so some familiarity with this language is expected.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "The AdvanceFSM
class basically manages all the FSMState
(s) implemented, and keeps updated with the transitions and the current state."
A block of code is set as follows:
using UnityEngine; using System.Collections; using System.Collections.Generic; public enum Transition { None = 0, SawPlayer, ReachPlayer, LostPlayer, NoHealth, }
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Our Tank object is basically a simple Mesh with a Rigidbody component."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title via 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 on 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 for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
We also provide you 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: http://www.packtpub.com/sites/default/files/downloads/3400OT_ColoredImages.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 would 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright 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.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.