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

Behavior trees


The behavior tree is another pattern used to represent and control the logic behind AI agents. They have become popular for the applications in AAA games such as Halo and Spore. Previously, we have briefly covered FSMs. They provide a very simple, yet efficient way to define the possible behaviors of an agent, based on the different states and transitions between them. However, FSMs are considered difficult to scale as they can get unwieldy fairly quickly and require a fair amount of manual setup. We need to add many states and hard-wire many transitions in order to support all the scenarios, which we want our agent to consider. So, we need a more scalable approach when dealing with large problems. This is where behavior trees come in.

Behavior trees are a collection of nodes, organized in a hierarchical order, in which nodes are connected to parents rather than states connected to each other, resembling branches on a tree, hence the name.

The basic elements of behavior trees are task nodes, where states are the main elements for FSMs. There are a few different tasks such as Sequence, Selector, and Parallel Decorator. It can be a bit daunting to track what they all do. The best way to understand this is to look at an example. Let's break the following transitions and states into tasks, as shown in the following figure:

Let's look at a Selector task for this behavior tree. Selector tasks are represented with a circle and a question mark inside. The selector will evaluate each child in order, from left to right. First, it'll choose to attack the player; if the Attack task returns success, the Selector task is done and will go back to the parent node, if there is one. If the Attack task fails, it'll try the Chase task. If the Chase task fails, it'll try the Patrol task. The following figure shows the basic structure of this tree concept:

Test is one of the tasks in the behavior trees. The following diagram shows the use of Sequence tasks, denoted by a rectangle with an arrow inside it. The root selector may choose the first Sequence action. This Sequence action's first task is to check whether the player character is close enough to attack. If this task succeeds, it'll proceed with the next task, which is to attack the player. If the Attack task also returns successfully, the whole sequence will return as a success, and the selector is done with this behavior, and will not continue with other Sequence tasks. If the proximity check task fails, the Sequence action will not proceed to the Attack task, and will return a failed status to the parent selector task. Then the selector will choose the next task in the sequence, Lost or Killed Player? The following figure demonstrates this sequence:

The other two common components are parallel tasks and decorators. A parallel task will execute all of its child tasks at the same time, while the Sequence and Selector tasks only execute their child tasks one by one. Decorator is another type of task that has only one child. It can change the behavior of its own child's tasks that includes whether to run its child's task or not, how many times it should run, and so on. We'll study how to implement a basic behavior tree system in Unity in Chapter 6, Behavior Trees.