Unity Artificial Intelligence Programming
eBook - ePub
No longer available

Unity Artificial Intelligence Programming

Add powerful, believable, and fun AI entities in your game with the power of Unity 2018!, 4th Edition

  1. 246 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub
No longer available

Unity Artificial Intelligence Programming

Add powerful, believable, and fun AI entities in your game with the power of Unity 2018!, 4th Edition

About this book

Learn and Implement game AI in Unity 2018 to build smart game environments and enemies with A*, Finite State Machines, Behavior Trees and NavMesh.

Key Features

  • Build richer games by learning the essential concepts in AI for games like Behavior Trees and Navigation Meshes
  • Implement character behaviors and simulations using the Unity Machine Learning toolkit
  • Explore the latest Unity 2018 features to make implementation of AI in your game easier

Book Description

Developing Artificial Intelligence (AI) for game characters in Unity 2018 has never been easier. Unity provides game and app developers with a variety of tools to implement AI, from the 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 your game's worlds and characters.

This fourth edition with Unity will help you break down AI into simple concepts to give you 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.

Further on, you'll learn how to distinguish the state machine pattern and implement one of your own. This is 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'll learn how to use Unity's built-in NavMesh feature and implement your own A* pathfinding system. You'll then learn how to implement simple flocks and crowd dynamics, which are key AI concepts in Unity. Moving on, you'll learn how to implement a behavior tree through a game-focused example. Lastly, you'll apply all the concepts in the book to build a popular game.

What you will learn

  • Create smarter game worlds and characters with C# programming
  • Apply automated character movement using pathfinding and steering behaviors
  • Implement non-player character decision-making algorithms using Behavior Trees and FSMs
  • Build believable and highly efficient artificial flocks and crowds
  • Create sensory systems for your AI with the most commonly used techniques
  • Construct decision-making systems to make agents take different actions
  • Explore the application of machine learning in Unity

Who this book is for

This book is intended for Unity developers with a basic understanding of C# and the Unity editor. Whether you're looking to build your first game or are looking to expand your knowledge as a game programmer, you will find plenty of exciting information and examples of game AI in terms of concepts and implementation.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Unity Artificial Intelligence Programming by Dr. Davide Aversa, Aung Sithu Kyaw, Clifford Peters in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Games. We have over one million books available in our catalogue for you to explore.

Information

Randomness and Probability

In this chapter, we are going to look at how we can apply the concepts of probability and randomness to game AI. This chapter is more about generic game AI development techniques in the areas of randomness and probability, and less about Unity3D in particular. This means that we can apply the concepts of this chapter to any game development middleware or technology framework. We'll be using mono C# in Unity3D for the demos mainly using the console to output data, and won't address much on the specific features of the Unity3D engine and the editor itself.
Game developers use probability to add a little uncertainty to the behaviors of AI characters, as well as to the wider game world. Randomness makes the artificial intelligence system less predictable and provides a more exciting, challenging, and fair experience.
In this chapter, we will look at the following topics:
  • We take look at randomness and probability.
  • We will be creating a simple dice game. We will also give some examples of applications for probability and dynamic AI.
  • We will finish the chapter with a simple demo slot machine.

Randomness in games

Game designers and developers use randomness in game AI to make the game and characters more realistic and varied by not making the same decision or taking the same action again and again.
Let's take an example of a typical soccer game. One of the rules of a soccer game is to award a direct free kick if one player commits a foul while trying to possess the ball from the opposing team. Now, instead of giving a foul and a free kick all the time whenever that foul happens, the game developer can apply a probability so that the game rewards with a direct freekick only 98 percent of all the fouls. As a result, most of the time, the player gets a direct freekick; but when that remaining two percent happens, it can provide emotional feedback to the players from both the teams (assuming that you are playing against another human). The other player would feel angry and disappointed, while you'd feel lucky and satisfied. After all, the referees are human, and like all other humans, they might not be 100 percent correct all the time.
But how can a computer produce random values? And how can we use them in Unity?

Randomness in computer science

Computers are deterministic machines: by design, if we give a computer the same input multiple times, in the form of program code and data, then it returns the same output. Therefore, how can we have a program return unpredictable and random outputs?
If we need truly random numbers, then we need to take this randomness from somewhere else. That's why many advanced applications try to combine different external sources of randomness into a random value: they may look at the movement of the mouse in a certain interval, to the noise of the internet connection, or even ask the user to smash the keyboard randomly, and so on. There is even dedicated hardware for random number generation!
Fortunately, in games, we do not need such truly random numbers, and can use simpler algorithms that can generate sequences that look like a sequence of random numbers. Such algorithms are called Pseudorandom Number Generators (PRNG): using an initial seed, they can generate, in a deterministic way, a sequence of numbers that statistically approximate the properties of a sequence of truly random numbers. The catch is that, if we start from the same seed, we always get the same sequence of numbers. For this reason, we usually initialize the seed value from something that we imagine is always different every time the user open the application. For instance, the elapsed time in milliseconds since the computer started running, or the number of milliseconds since 1970 (the Unix timestamp). Note, however, that having the possibility to obtain the same random sequence every time is really helpful when debugging!
Finally, note that some PRNGs are more random than others. If we were creating an encryption program, we would want to look into less predictable PRNGs, called Cryptographically Secure Pseudo-Random Number Generator (CSPRNG). Fortunately, for games, the simple RNG that comes with Unity is good enough.

The Unity Random class

The Unity3D script has a Random class to generate random data. You can set the generator seed using the InitState(int seed) function. Usually, we wouldn't want to seed the same value again and again, as this will result in the same predictable sequence of random numbers being generated. One of the reasons for keeping the same seed value is for testing purposes, or if you want your players to be able to generate a procedural map/level with a specific seed.
Then, you can read the Random.value property to get a random number between 0.0 and 1.0. This generator is inclusive, so both 0.0 and 1.0 may be returned by this property.
Another class method that could be quite handy is the Range method:
static function Range (min : float, max : float) : float
The Range method can be used to generate a random number from a range. When given an integer value, it returns a random integer number between min (inclusive) and max (exclusive). This means that a min may be returned, but never max. If you pass in float values for the range, it'll return a random float number between min (inclusive) and max (exclusive). Take note whenever a parameter is exclusive or inclusive: since the integer random value is exclusive of max in range, we'll need to pass in n+1 as the max range if we want n to be our desired maximum random integer. On the contrary, for the float random value, the max value in the range is inclusive.

Simple random dice game

Let's set up a very simple dice game in a new scene where a random number between 1 and 6 is generated, and checked against the input value. The player wins if the input value matches the dice result generated randomly, as shown in the following DiceGame.cs file:
public class DiceGame : MonoBehaviour {

public string inputValue = "1";

public Text outputText;
public InputField inputField;
public Butto...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. About Packt
  5. Contributors
  6. Preface
  7. Introduction to AI
  8. Finite State Machines
  9. Randomness and Probability
  10. Implementing Sensors
  11. Flocking
  12. Path-Following and Steering Behaviors
  13. A* Pathfinding
  14. Navigation Mesh
  15. Behavior Trees
  16. Machine Learning in Unity
  17. Putting It All Together
  18. Other Books You May Enjoy