Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Unity 2018 Artificial Intelligence Cookbook
Unity 2018 Artificial Intelligence Cookbook

Unity 2018 Artificial Intelligence Cookbook: Over 90 recipes to build and customize AI entities for your games with Unity , Second Edition

Arrow left icon
Profile Icon Jorge Elieser P Garrido Profile Icon Jorge Palacios
Arrow right icon
€29.99
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.4 (5 Ratings)
eBook Aug 2018 334 pages 2nd Edition
eBook
€29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Jorge Elieser P Garrido Profile Icon Jorge Palacios
Arrow right icon
€29.99
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.4 (5 Ratings)
eBook Aug 2018 334 pages 2nd Edition
eBook
€29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Unity 2018 Artificial Intelligence Cookbook

Navigation

In this chapter, we will cover the following recipes:

  • Representing the world with grids
  • Representing the world with points of visibility
  • Representing the world with a self-made navigation mesh
  • Finding your way out of a maze with DFS
  • Finding the shortest path in a grid with BFS
  • Finding the shortest path with Dijkstra
  • Finding the best-promising path with A*
  • Improving A* for memory: IDA*
  • Planning navigation in several frames: time-sliced search
  • Smoothing a path

Introduction

In this chapter, we will learn path-finding algorithms for navigating complex scenarios. Game worlds are usually complex structures, be it a maze, an open world, and everything in between. That's why we need different techniques for approaching these kinds of problems.

We'll learn some ways of representing the world using different kinds of graph structures, and several algorithms for finding a path, each aimed at different situations.

It is worth mentioning that path-finding algorithms rely on techniques such as Seek and Arrive, learned in the previous chapter, in order to navigate the map.

Representing the world with grids

A grid is the most widely-used structure for representing worlds in games because it is easy to implement and visualize. However, we will lay the foundations for advanced graph representations while learning the basis of graph theory and its properties.

Getting ready

First, we need to create an abstract class, Graph, declaring the virtual methods that every graph representation implements. It is done this way because no matter how the vertices and edges are represented internally, the path-finding algorithms remain high level, thereby avoiding implementation of the algorithms for each type of graph representation.

This class works as a parent class for the different representations to be learned...

Representing the world with points of visibility

This is another widely-used technique for world representation based on points located throughout the valid area of navigation, be they manually placed or automated via scripting. We'll be using manually placed points connected automatically via scripting.

Getting ready

Just like the previous representation, it's important to have several things in order before continuing:

  • Having the Edge class prepended to the Graph class in the same file
  • Defining the GetEdges function in the Graph class
  • Having the Vertex class
The vertex objects in the scene must have a collider component attached to them, as well as the Vertex tag assigned. It's recommended that they are...

Representing the world with a self-made navigation mesh

Sometimes, a custom navigation mesh is necessary for dealing with difficult situations such as different types of graphs, but placing the graph's vertices manually is troublesome because it requires a lot of time to cover large areas.

We will learn how to use a model's mesh in order to generate a navigation mesh based on its triangles' centroids as vertices, and then leverage the heavy lifting we learned from the previous recipe.

Getting ready

This recipe requires some knowledge of custom editor scripting and an understanding of implementing the points of visibility graph representation. Also, it is worth mentioning that the script instantiates a CustomNavMesh...

Finding your way out of a maze with DFS

The Depth-First Search (DFS) algorithm is a path-finding technique suitable for low-memory devices. Another common use is to build mazes with some minor modifications to the list of nodes visited and discovered, but the main algorithm stays just the same.

Getting ready

This is a high-level algorithm that relies on each graph's implementation of the main functions (build, init, and so on), so that the algorithm is implemented in the Graph class.

It is important to take a moment and verify when the recipe is manipulating actual objects, or vertex IDs.

How to do it...

...

Finding the shortest path in a grid with BFS

The Breadth-First Search (BFS) algorithm is just another basic technique for graph traversal and is aimed at getting the shortest path in the fewest steps possible, with the trade-off of being expensive in memory; thus, it is aimed especially at games for high-end consoles and computers.

Getting ready

This is a high-level algorithm that relies on each graph's implementation of the general functions, so the algorithm is implemented in the Graph class.

How to do it...

Even though this recipe is only defining a function, please...

Finding the shortest path with Dijkstra

Dijkstra's algorithm was initially designed to solve the single-source shortest path problem for a graph. Thus, the algorithm finds the lowest-cost route to everywhere from a single point. We will learn how to make use of it given two different approaches.

Getting ready

The first thing to do is to import the binary heap class from the Game Programming Wiki (GPWiki) into our project, given that neither the .Net framework nor Mono have a defined structure for handling binary heaps or priority queues.

The source file is already in the book's online repository service provider as it is no longer available online.

...

Finding the best-promising path with A*

The A* algorithm is probably the most widely-used technique for path finding given its implementation simplicity, efficiency, and because it has room for optimization. It's no coincidence that there are several algorithms based on it. At the same time, A* shares several roots with Dijkstra's algorithm, so you'll find similarities in their implementations.

Getting ready

Just like Dijkstra's algorithm, this recipe uses the binary heap extracted from the GPWiki. Also, it is important to understand what delegates are and how they work. Finally, we are entering into the world of informed search; that means that we need to understand what a heuristic is and what it is for...

Improving A* for memory – IDA*

IDA* is a variant of an algorithm called Iterative Deepening Depth-First Search. Its memory usage is lower than A* because it doesn't make use of data structures to store the looked-up and explored nodes.

Getting ready

For this recipe, it is important to have some understanding of how recursion works.

How to do it...

This is a long recipe that can be regarded as a big two-step process: creating the main function and an internal recursive one. Please take into consideration the comments in the code for better understanding of the...

Planning navigation in several frames – time-sliced search

When dealing with large graphs, computing paths can take a lot of time, even driving the game to a halt for a couple of seconds. This could lead to ruining the overall experience, to say the least. Luckily enough, there are methods for avoiding that.

This recipe is built on top of the principles of using coroutines as a method for keeping the game running smoothly while finding a path in the background; some knowledge of coroutines is required.

Getting ready

We'll learn how to implement path-finding techniques using coroutines by refactoring the A* algorithm learned previously, but we will handle its signature as a different function.

...

Smoothing a path

When dealing with regular-size vertices on a graph, such as grids, it's pretty common to see some kind of robotic movement from the agents in the game. Depending on the type of game we're developing, this could be avoided using path-smoothing techniques, such as the one we're about to learn:

Getting ready

Let's define a new tag in the Unity editor called Wall and assign it to every object in the scene that is intended to work as a wall or obstacle in the navigation.

How to do it...

This is an easy, yet powerful, function:

  1. Define...
Left arrow icon Right arrow icon

Key benefits

  • Explore different algorithms for creating decision-making agents that go beyond simple behaviors and movement
  • Discover the latest features of the NavMesh API for scripting intelligent behaviour in your game characters
  • Create games that are non-predictable and dynamic and have a high replayability factor

Description

Interactive and engaging games come with intelligent enemies, and this intellectual behavior is combined with a variety of techniques collectively referred to as Artificial Intelligence. Exploring Unity's API, or its built-in features, allows limitless possibilities when it comes to creating your game's worlds and characters. This cookbook covers both essential and niche techniques to help you take your AI programming to the next level. To start with, you’ll quickly run through the essential building blocks of working with an agent, programming movement, and navigation in a game environment, followed by improving your agent's decision-making and coordination mechanisms – all through hands-on examples using easily customizable techniques. You’ll then discover how to emulate the vision and hearing capabilities of your agent for natural and humanlike AI behavior, and later improve the agents with the help of graphs. This book also covers the new navigational mesh with improved AI and pathfinding tools introduced in the Unity 2018 update. You’ll empower your AI with decision-making functions by programming simple board games, such as tic-tac-toe and checkers, and orchestrate agent coordination to get your AIs working together as one. By the end of this book, you’ll have gained expertise in AI programming and developed creative and interactive games.

Who is this book for?

The Unity 2018 Artificial Intelligence Cookbook is for you if you are eager to get more tools under your belt to solve AI- and gameplay-related problems. Basic knowledge of Unity and prior knowledge of C# is an advantage.

What you will learn

  • Create intelligent pathfinding agents with popular AI techniques such as A and Ambush
  • Implement different algorithms for adding coordination between agents and tactical algorithms for different purposes
  • Simulate senses so agents can make better decisions, taking account of the environment
  • Explore different algorithms for creating decision-making agents that go beyond simple behaviors and movement
  • Create coordination between agents and orchestrate tactics when dealing with a graph or terrain
  • Implement waypoints by making a manual selector

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 28, 2018
Length: 334 pages
Edition : 2nd
Language : English
ISBN-13 : 9781788625227
Vendor :
Unity Technologies
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Aug 28, 2018
Length: 334 pages
Edition : 2nd
Language : English
ISBN-13 : 9781788625227
Vendor :
Unity Technologies
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 119.97
Unity 2018 Artificial Intelligence Cookbook
€36.99
Unity 2018 By Example
€36.99
Unity 2018 Cookbook
€45.99
Total 119.97 Stars icon

Table of Contents

11 Chapters
Behaviors - Intelligent Movement Chevron down icon Chevron up icon
Navigation Chevron down icon Chevron up icon
Decision Making Chevron down icon Chevron up icon
The New NavMesh API Chevron down icon Chevron up icon
Coordination and Tactics Chevron down icon Chevron up icon
Agent Awareness Chevron down icon Chevron up icon
Board Games and Applied Search AI Chevron down icon Chevron up icon
Learning Techniques Chevron down icon Chevron up icon
Procedural Content Generation Chevron down icon Chevron up icon
Miscellaneous Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.4
(5 Ratings)
5 star 20%
4 star 0%
3 star 0%
2 star 60%
1 star 20%
Fernando Lovera Oct 12, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I had a lot of fun reading this book, very interesting and simple explained, which shows good understanding of the author about these subjects.
Amazon Verified review Amazon
Brian Feb 19, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I simply can not recommend this book, especially for the price. It may be worth buying used for around $15 to serve as a code base. For a cookbook, it does a very poor job of explaining the specific problem that a particular chunk of code is designed to solve. This is especially true at the start of the book. I found myself reading through a lot of code that was just thrown at me with very little introduction... wondering what type of game world and what specific problem in that game world is this code designed to address. Usually cookbooks are organized by more specific objectives. The other side of the code block (where one normally expects an explanation of the code, reasoning behind why something was done this way or that, and how it may need to be modified to fit certain situations) was equally barren. With all of that said, there is some good example code in there if you are willing to do most of the hard work yourself in understanding how to implement it in your specific game project (hence the two star rating as opposed to one). But that is the key problem, example code and basic patterns can be found for free all over the Internet for free. A book is supposed to help to clarify things, put it in the clear context of an overarching theme, and help the reader adapt things to match their specific needs.
Amazon Verified review Amazon
Gabriel Campbell Mar 09, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
this guy just copy pasted the AI chapters he wrote for the Complete Unity 2018 Game Development book, which is about the cost of this book alone. Or maybe it went the other direction Either way, just getting that book is a much better deal. His examples are all pretty basic stuff, and for an AI focused book really doesn't get into anything advanced or useful on the subject. His examples are mostly without any greater context, and often without full implementation. There will be a lot of code samples where the method is just empty except one comment that says "//TODO implement your code here". Most of the book is taken up with re-implementing your own version of state machines, agent steering and the like, rather than making use of existing stuff in Unity that would let you jump into more advanced ideas. It could be useful if you are very new to both c# and unity and want a basic intro to the idea behind navigation and FSMs and so on, but even then there are better, free ai tutorials online.
Amazon Verified review Amazon
Gir the Game Designer Feb 23, 2020
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Doesn't explain anything in detail and turns out that you can get the same tech done with Behaviour Designer on the Asset Store
Amazon Verified review Amazon
Ariadne Feb 25, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I bought this book when there was just one (positive) review for it, and I wish I hadn’t. Each chapter consists of pages of code with no description about what to do with the scripts to make a working project. I downloaded the sample files and tried to run the project for the first chapter. It did run, but only because there are lots of other scripts in the project that are not mentioned in the book.The download itself is just a dump of all the scripts and prefabs for the whole book which makes it extremely difficult to work out which parts are needed for which chapter.It is a very discouraging book, not for a beginner or for anybody really. I am not new to AI (I have a MSc and a PhD in the subject) and I have made several Unity projects using other books and online resources, but this book has defeated me. I feel frustrated, disappointed and cheated and I cannot recommend this book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.