Unity 2018 Artificial Intelligence Cookbook - Jorge Palacios - E-Book

Unity 2018 Artificial Intelligence Cookbook E-Book

Jorge Palacios

0,0
34,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 277

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Unity 2018 Artificial Intelligence CookbookSecond Edition
Over 90 recipes to build and customize AI entities for your games with Unity
Jorge Palacios
BIRMINGHAM - MUMBAI

Unity 2018 Artificial Intelligence Cookbook Second Edition

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Kunal ChaudhariAcquisition Editor: Akshay GhadiContent Development Editor: Flavian VazTechnical Editor: Sachin SunilkumarCopy Editor:Safis EditingProject Coordinator:Devanshi DoshiProofreader: Safis EditingIndexer:Priyanka DhadkeGraphics:Jason MonteiroProduction Coordinator:Shraddha Falebhai

First published: March 2016 Second edition: August 2018

Production reference: 1240818

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78862-617-0

www.packtpub.com

To my parents, Betty and Elieser. They have given me all their support for several years, and believed in me every step of the way. Words cannot express how grateful I am for having them in my life. This work is proof that dreams can be achieved when you turn them into goals.
Thanks to my partner in bits and codes, Christian Chomiak. His support and feedback is always appreciated on every adventure, and every algorithm. Let's co-author the next book!
“If I have seen further it is by standing on the shoulders of giants.” – Isaac Newton
I'll be forever grateful to Ricardo Monascal and Carlos Pérez. Their support, friendship, and guidance allowed me to become the developer that I am today. I have seen further because of you.
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the author

Jorge Palacios is a software and game developer with a BS in computer science and eight years of professional experience. He's been developing games for the last five years in different roles, from tool developer to lead programmer. Mainly focused on artificial intelligence and gameplay programming, he is currently working with Unity and HTML5. He's also a game-programming instructor, speaker, and game-jam organizer.

About the reviewer

Dr. Davide Aversa holds a PhD in artificial intelligence and an MSc in artificial intelligence and robotics from the University of Rome, "La Sapienza," in Italy. He has a strong interest in artificial intelligence for the development of interactive virtual agents and Procedural Content Generation (PCG). He serves as a peer reviewer in video game-related conferences, such as the IEEE conference on Computational Intelligence and Games (CIG) and he also regularly participates in game-jam contests.

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Unity 2018 Artificial Intelligence Cookbook Second Edition

Dedication

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the author

About the reviewer

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Sections

Getting ready

How to do it...

How it works...

There's more...

See also

Get in touch

Reviews

Behaviors - Intelligent Movement

Introduction

Creating the behaviors template

Getting ready

How to do it...

How it works...

There's more...

See also

Pursuing and evading

Getting ready

How to do it...

How it works...

Adjusting the agent for physics

Getting ready

How to do it...

How it works...

See also

Arriving and leaving

Getting ready

How to do it...

How it works...

Facing objects

Getting ready

How to do it...

How it works...

Wandering around

Getting ready

How to do it...

How it works...

Following a path

Getting ready

How to do it...

How it works...

There's more...

Avoiding agents

Getting ready

How to do it...

How it works...

There's more...

Avoiding walls

Getting ready

How to do it...

How it works...

There's more...

See also

Blending behaviors by weight

Getting ready

How to do it...

How it works...

There's more...

See also

Blending behaviors by priority

Getting ready

How to do it...

How it works...

There's more...

See also

Shooting a projectile

Getting ready

How to do it...

How it works...

There's more...

See also

Predicting a projectile's landing spot

Getting ready

How to do it...

How it works...

There's more...

Targeting a projectile

Getting ready

How to do it...

How it works...

There's more...

Creating a jump system

Getting ready

How to do it...

How it works...

Navigation

Introduction

Representing the world with grids

Getting ready

How to do it...

How it works...

There's more...

See also

Representing the world with points of visibility

Getting ready

How to do it...

How it works...

There's more...

Representing the world with a self-made navigation mesh

Getting ready

How to do it...

How it works...

Finding your way out of a maze with DFS

Getting ready

How to do it...

How it works...

There's more...

Finding the shortest path in a grid with BFS

Getting ready

How to do it...

How it works...

There's more...

Finding the shortest path with Dijkstra

Getting ready

How to do it...

How it works...

There's more...

Finding the best-promising path with A*

Getting ready

How to do it...

How it works...

There's more...

See also

Improving A* for memory – IDA*

Getting ready

How to do it...

How it works...

There's more...

Planning navigation in several frames – time-sliced search

Getting ready

How to do it...

How it works...

See also

Smoothing a path

Getting ready

How to do it...

How it works...

Decision Making

Introduction

Choosing through a decision tree

Getting ready

How to do it...

How it works...

There's more...

Implementing a finite-state machine

Getting ready

How to do it...

How it works...

There's more...

Improving FSMs: hierarchical finite-state machines

Getting ready

How to do it...

How it works...

See also

Implementing behavior trees

Getting ready

How to do it...

How it works...

See also

Working with fuzzy logic

Getting ready

How to do it...

How it works...

There's more...

See also

Making decisions with goal-oriented behaviors

Getting ready

How to do it...

How it works...

Implementing a blackboard architecture

Getting ready

How to do it...

How it works...

There's more...

Experimenting with Unity's animation state machine

Getting ready

How to do it...

How it works...

There's more...

The New NavMesh API

Introduction

Setting up the NavMesh building components

Getting ready

How to do it...

How it works...

There's more...

See also

Creating and managing NavMesh for multiple types of agents

Getting ready

How to do it...

How it works...

There's more...

Creating and updating NavMesh data at runtime

Getting ready

How to do it...

How it works...

Controlling the lifetime of the NavMesh instance

Getting started

How to do it...

How it works...

There's more...

Connecting multiple instances of NavMesh

Getting ready

How to do it...

How it works...

There's more...

Creating dynamic NavMeshes with obstacles

Getting ready

How to do it...

How it works...

There's more...

See also

Implementing some behaviors using the NavMesh API

Getting ready

How to do it...

How it works...

There's more...

Coordination and Tactics

Introduction

Handling formations

Getting ready

How to do it...

How it works...

There's more...

See also

Extending A* for coordination – A*mbush

Getting ready

How to do it...

How it works...

There's more...

Analyzing waypoints by height

Getting ready

How to do it...

How it works...

Analyzing waypoints by cover and visibility

Getting ready

How to do it...

How it works...

Creating waypoints automatically

Getting ready

How to do it...

How it works...

There's more...

See also

Exemplifying waypoints for decision making

Getting ready

How to do it...

How it works...

See also

Implementing influence maps

Getting ready

How to do it...

How it works...

There's more...

See also

Improving influence with map flooding

Getting ready

How to do it...

How it works...

See also

Improving influence with convolution filters

Getting ready

How to do it...

How it works...

There's more...

See also

Building a fighting circle

Getting ready

How to do it...

How it works...

There's more...

See also

Agent Awareness

Introduction

The seeing function using a collider-based system

Getting ready

How to do it...

How it works...

The hearing function using a collider-based system

Getting ready

How to do it...

How it works...

There's more...

The smelling function using a collider-based system

Getting ready

How to do it...

How it works...

There's more...

The seeing function using a graph-based system

Getting ready

How to do it...

How it works...

The hearing function using a graph-based system

Getting ready

How to do it...

How it works...

There's more...

See also

The smelling function using a graph-based system

Getting ready

How to do it...

How it works...

See also

Creating awareness in a stealth game

Getting ready

How to do it...

How it works...

There's more...

See also

Board Games and Applied Search AI

Introduction

Working with the game-tree class

Getting ready...

How to do it...

How it works...

See also

Implementing Minimax

Getting ready...

How to do it...

How it works...

See also

Implementing Negamax

Getting ready...

How to do it...

How it works...

There's more...

See also

Implementing AB Negamax

Getting ready...

How to do it...

How it works...

See also

Implementing NegaScout

Getting ready...

How to do it...

How it works...

See also

Implementing a Tic-Tac-Toe rival

Getting ready...

How to do it...

How it works...

There's more...

See also

Implementing a Checkers rival

Getting ready...

How to do it...

How it works...

There's more...

Implementing Rock-Paper-Scissors AI with UCB1

Getting ready...

How to do it...

How it works...

There's more...

See also

Implementing regret matching

Getting ready...

How to do it...

How it works...

There's more...

See also

Learning Techniques

Introduction

Predicting actions with an N-Gram predictor

Getting ready...

How to do it...

How it works...

There's more...

Improving the predictor – Hierarchical N-Gram

Getting ready...

How to do it...

How it works...

Learning to use Naïve Bayes classifier

Getting ready...

How to do it...

How it works...

Implementing reinforcement learning

Getting ready...

How to do it...

How it works...

Implementing artificial neural networks

Getting ready...

How to do it...

How it works...

Procedural Content Generation

Introduction

Creating mazes with Depth-First Search

Getting ready

How to do it...

How it works...

Implementing the constructive algorithm for dungeons and islands

Getting ready

How to do it...

How it works...

There's more...

See also

Generating landscapes

Getting ready

How to do it...

How it works...

Using N-Grams for content generation

Getting ready

How to do it...

How it works...

There's more...

See also

Generating enemies with the evolutionary algorithm

Getting ready

How to do it...

How it works...

There's more...

See also

Miscellaneous

Introduction

Creating and managing Scriptable Objects

Getting ready

How to do it...

How it works...

There's more...

See also

Handling random numbers better

Getting ready

How to do it...

There's more...

See also

Building an air-hockey rival

Getting ready

How to do it...

How it works...

See also

Implementing an architecture for racing games

Getting ready

How to do it...

How it works...

Managing race difficulty using a rubber-band system

Getting ready

How to do it...

How it works...

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

When we think about artificial intelligence (AI), a lot of topics may come to mind, from simple behaviors such as following or escaping from the player, through the classic chess-rival AI, to state-of-the-art techniques in machine learning or procedural content generation.

Talking about Unity means talking about game development democratization. Thanks to its ease of use, fast-paced technological improvements, ever-growing community of developers, and new cloud services, Unity has become one of the most important pieces of game industry software.

With all that in mind, the main goal of writing this book is to offer you, the reader, both technical insight into Unity, following best practices and conventions, and theoretical knowledge that helps you grasp AI concepts and techniques, so you can get the best of both worlds for your own personal and professional development.

This cookbook will introduce you to the tools you need to build great AI, either for creating better enemies, polishing that final boss, or even building your own customized AI engine. It aims to be your one-stop reference for developing AI techniques in Unity.

Welcome to an exciting journey that combines a variety of things that means a lot to me as a professional and human being; programming, game development, AI, and sharing knowledge with other developers. I cannot stress how humbled and happy I am to be read by you right now, and grateful to the team at Packt for this formidable opportunity. I hope this material helps you not only take your Unity and AI skills to new levels, but also deliver that feature that will engage players with your game.

Who this book is for

This book is intended for those who already have a basic knowledge of Unity and are eager to get more tools under their belt to solve AI and gameplay-related problems.

What this book covers

Chapter 1, Behaviors – Intelligent Movement, explores some of the most interesting movement algorithms based on the steering behavior principles developed by Craig Reynolds and work from Ian Millington. They act as a foundation for most of the AI used in advanced games and other algorithms that rely on movement, such as the family of path-finding algorithms.

Chapter 2, Navigation, covers path-finding algorithms for navigating complex scenarios. It will include some ways of representing the world using different kinds of graph structure, and several algorithms for finding a path, each aimed to different situations.

Chapter 3, Decision Making, explains different decision-making techniques that are flexible enough to adapt to different types of games, and robust enough to let us build modular decision-making systems.

Chapter 4, The New NavMesh API, shows the inner workings of the NavMesh API introduced in Unity 5.6, and explains how it enables us to grasp the power of the NavMesh engine and tune it in real time.

Chapter 5, Coordination and Tactics, deals with a number of different recipes for coordinating different agents as a whole organism, such as formations and techniques that allow us make tactical decisions based on graphs, such as waypoints and influence maps.

Chapter 6, Agent Awareness, explores different ways to simulate sense stimuli on an agent. We will learn how to use tools we already know to create these simulations: colliders and graphs.

Chapter 7, Board Games and Applied Search AI, covers a family of algorithms for developing board games, as well as turn-based-game techniques for creating AI.

Chapter 8, Learning Techniques, explores the field of machine learning. It will give us a great head-start in our endeavor to learn and apply machine learning techniques into our games.

Chapter 9, Procedural Content Generation, explores different techniques for enabling replayability in our games by creating content procedurally. It will give us some pointers in the right direction for different types of content.

Chapter 10, Miscellaneous, introduces new techniques and uses algorithms that we will have learned in previous chapters to create new behaviors that don't quite fit in a definite category.

To get the most out of this book

Anyone with a strong programming background will be able to get the most out of this book. However, readers that also have strong foundations in comp-sci will benefit greatly from the set of implementations in Unity.

Before you start, it is important to know about programming, data structures, and the basic foundations of C#. We assume that you feel comfortable creating scripts components in Unity, and have developed some prototypes in the past.

We believe that you will get the most out of this book if you already have an idea of the modules for beginner and intermediate gameplay scripting available at https://unity3d.com/learn/tutorials/s/scripting.

We have developed this book using Unity, Visual Studio Community, and Visual Studio Code. The latter shows better performance and behaves in the same way in Windows and Mac operating systems. The first would be our choice for a Windows-only development environment.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Unity-2018-Artificial-Intelligence-Cookbook-Second-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/Unity2018ArtificialIntelligenceCookbookSecondEdition_ColorImages.pdf.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it...

This section contains the steps required to follow the recipe.

How it works...

This section usually consists of a detailed explanation of what happened in the previous section.

There's more...

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

Behaviors - Intelligent Movement

In this chapter, we will develop AI algorithms for movement by covering the following recipes:

Creating the behaviors template

Pursuing and evading

Adjusting the agent for physics

Arriving and leaving

Facing objects

Wandering around

Following a path

Avoiding agents

Avoiding walls

Blending behaviors by weight

Blending behaviors by priority

Shooting a projectile

Predicting a projectile's landing spot

Targeting a projectile

Creating a jump system

Introduction

Unity has been one of the most popular game engines for quite a while now, and it's probably the de facto game development tool for indie developers, not only because of its business model, which has a low entry barrier, but also because of its robust project editor, year-by-year technological improvements, and, most importantly, its ease of use and an ever-growing community of developers around the globe.

Thanks to Unity's heavy lifting behind the scenes (rendering, physics, integration, and cross-platform deployment, to name but a few) it's possible for us to focus on creating the AI systems that will bring our games to life, creating great real-time experiences in the blink of an eye.

The goal of this book is to give you the tools to build great AI, create better enemies, polish that final boss, or even build your own customized AI engine.

In this chapter, we will start by exploring some of the most interesting movement algorithms based on the steering behavior principles developed by Craig Reynolds, along with work from Ian Millington. These recipes are the stepping stones for most of the AI used in advanced games and other algorithms that rely on movement, such as the family of path-finding algorithms.

Creating the behaviors template

Before creating our behaviors, we need to code the stepping stones that help us to create not only intelligent movement, but also help us to build a modular system to change and add these behaviors. We will create custom data types and base classes for most of the algorithms covered in this chapter.

Getting ready

Our first step is to remember the update functions' order of execution:

Update

LateUpdate

How it works...

The idea is to be able to delegate the movement's logic inside the GetSteering() function on the behaviors that we will later build, simplifying our agent's class to a main calculation based on those.

Besides, we are guaranteed to be able to set the agent's steering value before it is used thanks to Unity script and function execution orders.

There's more...

This is a component-based approach, which means that we must remember to always have an Agent script attached to GameObject for the behaviors to work as expected.

See also

For further information on Unity's game loop and the execution order of functions and scripts, please refer to the official documentation available online at these links:

http://docs.unity3d.com/Manual/ExecutionOrder.html

https://docs.unity3d.com/Manual/class-MonoManager.html

Pursuing and evading

Pursuing and evading are great behaviors to start with because they rely on the most basic behaviors and extend their functionality by predicting the target's next step.