Book Image

Unity 4.x Game AI Programming

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

Unity 4.x Game AI Programming

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

Overview of this book

<p>This book fills the gap between artificial intelligence (AI) books designed to learn underlying AI algorithms and general Unity3D books written to cover basic scene setup and scripting in Unity3D. Game AI Scripting in Unity3D covers implementing AI techniques such as flocking, pathfinding, path following, and behavior trees in Unity3D with example projects.<br /><br />Game AI Scripting in Unity3D will show you how to apply AI techniques to your Unity3D projects using C# as the scripting language. Unlike other AI books and Unity3D books, this book tries to focus more on the application of AI techniques in the Unity3D engine, with sample projects that demonstrate finite state machines (FSMs), pathfinding, steering, navigation graphs, and behavior trees techniques. <br /><br />This book shows how to implement various AI techniques in Unity3D by implementing the algorithm from scratch using C#, applying Unity3D built-in features, or using available scripts and plugins from the Unity Asset Store. For example, we’ll be implementing our own A* algorithm to do pathfinding but will also explore the Unity3D navigation graphs feature. Then we’ll use the Behave plugin to construct behavior trees for intelligent AI character behaviors.<br /><br />Game AI Scripting in Unity3d&nbsp; covers other AI techniques such as flocking behavior, building a sensory system for taking inputs from the environment and other AI agents, and so on. In the final chapter this book will show you how to build a racing game AI project using Unity3D and applying the techniques described in earlier chapters.</p>
Table of Contents (17 chapters)
Unity 4.x Game AI Programming
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

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.

What this book covers

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.

What you need for this book

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.

Who this book is for

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.

Conventions

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."

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 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 , 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.

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 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.

Downloading the color images of this book

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

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 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

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 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

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.