O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unity Artificial Intelligence Programming - Fourth Edition

Book Description

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 ?ocks 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.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Unity Artificial Intelligence Programming Fourth Edition
  3. Dedication
  4. About Packt
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Introduction to AI
    1. Artificial Intelligence (AI)
    2. AI in games
    3. AI techniques
      1. Finite State Machines (FSMs)
      2. Random and probability in AI
      3. The sensor system
        1. Polling
        2. The messaging system
      4. Flocking, swarming, and herding
      5. Path following and steering
      6. A* pathfinding
      7. A navigation mesh
      8. The behavior trees
      9. Locomotion
    4. Summary
  8. Finite State Machines
    1. The player's tank
      1. Initialization
        1. Shooting bullet
        2. Controlling the tank
    2. The Bullet class
    3. Setting up waypoints
    4. The abstract FSM class
    5. The enemy tank AI
      1. The Patrol state
      2. The Chase state
      3. The Attack state
      4. The Dead state
        1. Taking damage
    6. Using an FSM framework
      1. The AdvanceFSM class
      2. The FSMState class
      3. The state classes
        1. The PatrolState class
      4. The NPCTankController class
    7. Summary
  9. Randomness and Probability
    1. Randomness in games
      1. Randomness in computer science
      2. The Unity Random class
        1. Simple random dice game
    2. Definitions of probability
      1. Independent and related events
      2. Conditional probability
        1. Loaded dice
    3. Character personalities
    4. FSM with probability
    5. Dynamic AI
    6. Demo slot machine
      1. Random slot machine
      2. Weighted probability
        1. Near miss
    7. Summary
    8. Further reading
  10. Implementing Sensors
    1. Basic sensory systems
    2. Scene setup
    3. The player's tank and the aspect class
      1. The player's tank
      2. Aspect
    4. AI characters
      1. Sense
      2. Sight
      3. Touch
    5. Testing
    6. Summary
  11. Flocking
    1. Basic flocking behavior
      1. Individual behavior
      2. Controller
    2. Alternative implementation
      1. FlockController
    3. Summary
  12. Path-Following and Steering Behaviors
    1. Following a path
      1. Path script
      2. Path-following agents
    2. Avoiding obstacles
      1. Adding a custom layer
      2. Obstacle avoidance
    3. Summary
  13. A* Pathfinding
    1. Revisiting the A* algorithm
    2. Implementing the A* algorithm
      1. Node
      2. PriorityQueue
      3. The GridManager class
      4. The AStar class
      5. The TestCode class
    3. Setting up the scene
    4. Testing the pathfinder
    5. Summary
  14. Navigation Mesh
    1. Setting up the map
      1. Navigation static
      2. Baking the navigation mesh
      3. NavMesh agent
        1. Updating an agents' destinations
          1. The Target.cs class
    2. Scene with slope
    3. Navigation areas
    4. Off Mesh Links
      1. Generated Off Mesh Links
      2. Manual Off Mesh Links
    5. Summary
  15. Behavior Trees
    1. Introduction to Behavior Trees
      1. A simple example – patrolling robot
    2. 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. Implement the Nodes
      5. Building the Tree
      6. Attach the BT to the Enemy
    3. Summary
    4. External Resources
  16. Machine Learning in Unity
    1. The Unity Machine Learning Agents Toolkit
    2. How to install the ML-Agents Toolkit
      1. Installing Python and TensorFlow on Windows
      2. Installing Python and TensorFlow on macOS and Unix-like systems
    3. Using the ML-Agents Toolkit – a basic example
      1. Creating the scene
      2. Implementing the code
      3. Adding the final touches
      4. Training a Brain object
      5. Training the agent
    4. Summary
    5. Further reading
  17. Putting It All Together
    1. Basic game structure
    2. Adding automated navigation
      1. Creating the NavMesh
      2. Setting up the agent
      3. Fixing the GameManager script
    3. Creating decision-making AI with FSM
    4. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think