Reader small image

You're reading from  Unity Artificial Intelligence Programming - Fifth Edition

Product typeBook
Published inMar 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781803238531
Edition5th Edition
Languages
Tools
Right arrow
Author (1)
Dr. Davide Aversa
Dr. Davide Aversa
author image
Dr. Davide Aversa

Dr. Davide Aversa holds a PhD in Artificial Intelligence (AI) and an MSc in AI and robotics from the University of Rome La Sapienza in Italy. He has a strong interest in AI for the development of interactive virtual agents and procedural content generation. He has served as a program committee member for video game-related conferences such as the IEEE conference on computational intelligence and games, and he also regularly participates in game-jam contests. He also writes a blog on game design and game development.
Read more about Dr. Davide Aversa

Right arrow

Preface

Developing Artificial Intelligence (AI) for game characters in Unity has never been easier. Unity provides game and app developers with a variety of tools to implement AI, from basic techniques to cutting-edge machine learning-powered agents. Leveraging these tools via Unity's API or built-in features allows limitless possibilities when it comes to creating game worlds and characters.

This updated fifth edition of Unity Artificial Intelligence Programming starts by breaking down AI into simple concepts. 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. As you progress, you'll learn how to implement a Finite State Machine (FSM) to determine how your AI behaves, apply probability and randomness to make games less predictable, and implement a basic sensory system. Later, you'll understand how to set up a game map with a navigation mesh, incorporate movement through techniques such as A* pathfinding, and provide characters with decision-making abilities using behavior trees.

By the end of this Unity book, you'll have the skills you need to bring together all the concepts and practical lessons you've learned to build an impressive vehicle battle game.

Who this book is for

This Unity AI book is for Unity developers with a basic understanding of C# and the Unity Editor who want to expand their knowledge of AI Unity game development.

What this book covers

Chapter 1, Introduction to AI, talks about what AI is and how it is used in games. We also discuss the various techniques used to implement AI in games.

Chapter 2, Finite State Machines, discusses a way of simplifying how we manage the decisions that 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, Randomness 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 making our characters aware of the world around them. With the ability for our characters to see and hear, they will know when an enemy is nearby and when to attack.

Chapter 5, Flocking, discusses the 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 having a predefined path, simply by moving toward a goal while avoiding obstacles as they appear.

Chapter 7, A* Pathfinding, discusses a popular algorithm 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 in a better way than we could by using tiles and the A* algorithm.

Chapter 9, Behavior Trees, teaches you about behavior trees, a popular decision-making technique for game AI. We will explore the general architecture of behavior trees and how to use them to control a simple agent. Then we will use the free plugin Behavior Bricks to apply our new knowledge to a simple mini-game project.

Chapter 10, Procedural Content Generation, explores the basics of generative AI and procedural content generation. We will see how to use Perlin noise to generate realistic terrain, and how to use Cellular Automata to generate a cave-like dungeon map.

Chapter 11, Machine Learning in Unity, explores how to apply machine learning (in particular, reinforcement learning) to game characters for games or simulations. We will use the official Unity ML-Agents Toolkit. In the first part, we will learn how to configure Unity and the external requirements for the toolkit. Then we will showcase two simple practical examples.

Chapter 12, Putting It All Together, takes various elements of what you have learned throughout the book and brings it all together into one last project. Here, you will apply the remaining AI elements you have learned and create an impressive vehicle battle game.

To get the most out of this book

For this book, you only need to install a recent version of Unity3D. The code projects in this book have been tested on macOS and Windows on Unity 2022 and Unity 2021 but, with minor adjustments, they should work with future releases too.

For Chapter 9, Behavior Trees, you will need to install the Behavior Bricks plugin for Unity. In Chapter 11, Machine Learning in Unity, we will install Python 3.7 and PyTorch.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book's GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Unity-Artificial-Intelligence-Programming-Fifth-Edition. If there's an update to the code, it will be updated in the GitHub repository.

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

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781803238531_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Let's start creating the PlayerTankController class by setting up the Start function and the Update function in the PlayerTankController.cs file."

A block of code is set as follows:

public class PlayerTankController : MonoBehaviour { 
    public GameObject Bullet; 
    public GameObject Turret; 
    public GameObject bulletSpawnPoint;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

// ... 
    private AudioSource m_ExplosionAudio  
    private ParticleSystem m_ExplosionParticles 
    private float m_CurrentHealth; 
    private bool m_Dead; 
    public float CurrentHealth { get; } 
    // ...

Any command-line input or output is written as follows:

git clone --branch release_19 https://github.com/Unity-Technologies/ml-agents.git

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: "If you play the scene and click on the Pull Lever button, you should see the final result."

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

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/support/errata and fill in the form.

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 copyright@packt.com 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.

Share Your Thoughts

Once you've read Unity Artificial Intelligence Programming – Fifth Edition, we'd love to hear your thoughts! Please select https://www.amazon.in/review/1803238534 for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Artificial Intelligence Programming - Fifth Edition
Published in: Mar 2022Publisher: PacktISBN-13: 9781803238531
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dr. Davide Aversa

Dr. Davide Aversa holds a PhD in Artificial Intelligence (AI) and an MSc in AI and robotics from the University of Rome La Sapienza in Italy. He has a strong interest in AI for the development of interactive virtual agents and procedural content generation. He has served as a program committee member for video game-related conferences such as the IEEE conference on computational intelligence and games, and he also regularly participates in game-jam contests. He also writes a blog on game design and game development.
Read more about Dr. Davide Aversa