Unity Artificial Intelligence Programming - Fifth Edition

Book description

Learn and implement game AI in Unity to build smart environments and enemies with A* pathfinding, finite state machines, behavior trees, and the NavMesh

Key Features

  • Explore the latest Unity features to make AI implementation in your game easier
  • Build richer and more dynamic games using AI concepts such as behavior trees and navigation meshes
  • Implement character behaviors and simulations using the Unity Machine Learning toolkit

Book Description

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.

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

What you will learn

  • Understand the basics of AI in game design
  • Create smarter game worlds and characters with C# programming
  • Apply automated character movement using pathfinding algorithm behaviors
  • Implement character decision-making algorithms using behavior trees
  • Build believable and highly efficient artificial flocks and crowds
  • Create sensory systems for your AI world
  • Become well-versed with the basics of procedural content generation
  • Explore the application of machine learning in Unity

Who this book is for

This Unity artificial intelligence 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.

Table of contents

  1. Unity Artificial Intelligence Programming Fifth Edition
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Part 1:Basic AI
  7. Chapter 1: Introduction to AI
    1. Understanding AI
    2. AI in video games
    3. AI techniques for video games
      1. Finite state machines
      2. Randomness and probability in AI
      3. The sensor system
      4. Flocking, swarming, and herding
      5. Path following and steering
      6. A* pathfinding
      7. Navigation meshes
      8. Behavior trees
      9. Locomotion
    4. Summary
  8. Chapter 2: Finite State Machines
    1. Technical requirements
    2. Implementing the player's tank
      1. Initializing the Tank object
      2. Shooting the bullet
      3. Controlling the tank
    3. Implementing a Bullet class
    4. Setting up waypoints
    5. Creating the abstract FSM class
    6. Using a simple FSM for the enemy tank AI
      1. The Patrol state
      2. The Chase state
      3. The Attack state
      4. The Dead state
      5. Taking damage
    7. Using an FSM framework
      1. The AdvancedFSM class
      2. The FSMState class
      3. The state classes
      4. The NPCTankController class
    8. Summary
  9. Chapter 3: Randomness and Probability
    1. Technical requirements
    2. Introducing randomness in Unity
      1. Randomness in computer science
      2. The Unity Random class
      3. A simple random dice game
    3. Learning the basics of probability
      1. Independent and correlated events
      2. Conditional probability
      3. Loaded dice
    4. Exploring more examples of probability in games
      1. Character personalities
      2. Perceived randomness
      3. FSM with probability
      4. Dynamically adapting AI skills
    5. Creating a slot machine
      1. A random slot machine
      2. Weighted probability
      3. A near miss
    6. Summary
    7. Further reading
  10. Chapter 4: Implementing Sensors
    1. Technical requirements
    2. Basic sensory systems
    3. Setting up our scene
    4. The player's tank and the aspect class
      1. The player's tank
      2. Aspect
    5. AI characters
      1. Sense
      2. Sight
      3. Touch
    6. Testing the game
    7. Summary
  11. Part 2:Movement and Navigation
  12. Chapter 5: Flocking
    1. Technical requirements
    2. Basic flocking behavior
      1. Individual behavior
      2. Controller
    3. Alternative implementation
      1. FlockController
    4. Summary
  13. Chapter 6: Path Following and Steering Behaviors
    1. Technical requirements
    2. Following a path
      1. Path script
      2. Path-following agents
    3. Avoiding obstacles
      1. Adding a custom layer
      2. Obstacle avoidance
    4. Summary
  14. Chapter 7: A* Pathfinding
    1. Technical requirements
    2. Revisiting the A* algorithm
    3. Implementing the A* algorithm
      1. Node
      2. PriorityQueue
      3. The GridManager class
      4. The AStar class
      5. The TestCode class
    4. Setting up the scene
      1. Testing the pathfinder
    5. Summary
  15. Chapter 8: Navigation Mesh
    1. Technical requirements
    2. Setting up the map
      1. Navigation static
      2. Baking the NavMesh
      3. NavMesh agent
      4. Updating an agent's destinations
    3. Setting up a scene with slopes
    4. Baking navigation areas with different costs
    5. Using Off Mesh Links to connect gaps between areas
      1. Generated Off Mesh Links
      2. Manual Off Mesh Links
    6. Summary
  16. Part 3:Advanced AI
  17. Chapter 9: Behavior Trees
    1. Technical requirements
    2. Introduction to BTs
      1. A simple example – a patrolling robot
    3. Implementing a BT in Unity with Behavior Bricks
      1. Set up the scene
      2. Implement a day/night cycle
      3. Design the enemy behavior
    4. Implementing the nodes
      1. Building the tree
      2. Attach the BT to the enemy
    5. Summary
    6. Further reading
  18. Chapter 10: Procedural Content Generation
    1. Technical requirements
    2. Understanding Procedural Content Generation in games
      1. Kinds of Procedural Content Generation
    3. Implementing a simple goblin name generator
      1. Generating goblin names
      2. Completing the goblin description
    4. Learning how to use Perlin noise
      1. Built-in Unity Perlin noise
    5. Generating random maps and caves
      1. Cellular automata
      2. Implementing a cave generator
      3. Rendering the generated cave
    6. Summary
    7. Further reading
  19. Chapter 11: Machine Learning in Unity
    1. Technical requirements
    2. The Unity Machine Learning Agents Toolkit
    3. Installing the ML-Agents Toolkit
      1. Installing Python and PyTorch on Windows
      2. Installing Python and PyTorch on macOS and Unix-like systems
    4. Using the ML-Agents Toolkit – a basic example
      1. Creating the scene
      2. Implementing the code
      3. Adding the final touches
    5. Testing the learning environment
    6. Training an agent
    7. Summary
    8. Further reading
  20. Chapter 12: Putting It All Together
    1. Technical requirements
    2. Developing the basic game structure
    3. Adding automated navigation
      1. Creating the NavMesh
      2. Setting up the Agent
      3. Fixing the GameManager script
    4. Creating decision-making AI with FSM
    5. Summary
    6. Why subscribe?
  21. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Unity Artificial Intelligence Programming - Fifth Edition
  • Author(s): Dr. Davide Aversa
  • Release date: March 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803238531