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 AI Game Programming - Second Edition

Book Description

Leverage the power of Unity 5 to create fun and unbelievable AI entities in your games!

About This Book

  • Compose richer games by learning the essential concepts in artificial intelligence with exciting examples
  • Explore the brand new Unity 5 features that make implementing artificial intelligence in your game easier than ever
  • Using this practical guide become a competent Unity 3D developer by learning AI techniques, methods and the applicability of AI

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. It does not require any prior technical knowledge of how game AI works.

What You Will Learn

  • Understand the basic terminology and concepts in game AI
  • Implement a basic finite state machine using state machine behaviors in Unity 5
  • Create sensory systems for your AI with the most commonly used techniques
  • Implement an industry-standard path-finding system and a navigation mesh with the Unity 5 NavMesh feature
  • Build believable and highly-efficient artificial flocks and crowds
  • Create a basic behavior tree to drive a character’s actions
  • Make your characters more engaging by implementing fuzzy logic concepts in your AI’s decision-making
  • Tie all the concepts together with examples and guides

In Detail

Unity 5 provides game and app developers with a variety of tools to implement artificial intelligence. 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. Whether you are developing traditional, serious, educational, or any other kind of game, understanding how to apply artificial intelligence can take the fun-factor to the next level!

This book helps you break down artificial intelligence into simple concepts to give the reader 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 5. Along the way, several tips and tricks are included to make the development of your own AI easier and more efficient.

Starting from covering the basic essential concepts to form a base for the later chapters in the book, you will learn to distinguish the state machine pattern along with implementing your own. This will be 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 will be taught how to use Unity’s built-in NavMesh feature and implement your own A* pathfinding system. Then you will learn how to implement simple flocks and crowd’s dynamics, the key AI concepts. Then moving on you will learn how a behavior tree works and its implementation. Next you will learn adding layer of realism by combining fuzzy logic concepts with state machines. Lastly, you learn applying all the concepts in the book by combining them in a simple tank game.

Style and approach

An easy-to-follow guide that is full of example implementations of the concepts and is accompanied by easy-to-understand demonstrations and explanations of the code and concepts.

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 code file.

Table of Contents

  1. Unity AI Game Programming Second Edition
    1. Table of Contents
    2. Unity AI Game Programming Second Edition
    3. Credits
    4. About the Authors
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. The Basics of AI in Games
      1. Creating the illusion of life
      2. Leveling up your game with AI
      3. Using AI in Unity
      4. Defining the agent
      5. Finite State Machines
      6. Seeing the world through our agent's eyes
      7. Path following and steering
        1. Using A* Pathfinding
        2. Using navigation mesh
      8. Flocking and crowd dynamics
      9. Behavior trees
      10. Thinking with fuzzy logic
      11. Summary
    9. 2. Finite State Machines and You
      1. Finding uses for FSMs
      2. Creating state machine behaviors
        1. Creating the AnimationController asset
        2. Layers and Parameters
        3. The animation controller inspector
        4. Bringing behaviors into the picture
        5. Creating our very first state
        6. Transitioning between states
      3. Setting up our player tank
      4. Creating the enemy tank
        1. Choosing transitions
        2. Making the cogs turn
          1. Setting conditions
          2. Driving parameters via code
          3. Making our enemy tank move
          4. Testing
      5. Summary
    10. 3. Implementing Sensors
      1. Basic sensory systems
        1. Cone of sight
        2. Hearing, feeling, and smelling using spheres
        3. Expanding AI through omniscience
        4. Getting creative with sensing
      2. Setting up the scene
      3. Setting up the player tank and aspect
        1. Implementing the player tank
        2. Implementing the Aspect class
      4. Creating an AI character
      5. Using the Sense class
      6. Giving a little perspective
      7. Touching is believing
      8. Testing the results
      9. Summary
    11. 4. Finding Your Way
      1. Following a path
        1. The path script
        2. Using the path follower
        3. Avoiding obstacles
        4. Adding a custom layer
        5. Implementing the avoidance logic
      2. A* Pathfinding
        1. Revisiting the A* algorithm
        2. Implementation
          1. Implementing the Node class
          2. Establishing the priority queue
          3. Setting up our grid manager
          4. Diving into our A* implementation
          5. Implementing a Test Code class
          6. Setting up our sample scene
          7. Testing all the components
      3. Navigation mesh
        1. Setting up the map
        2. Navigation Static
        3. Baking the navigation mesh
        4. Using the NavMesh agent
        5. Setting a destination
        6. The Target class
        7. Testing slopes
        8. Exploring areas
        9. Making sense of Off Mesh Links
        10. Using the generated Off Mesh Links
        11. Setting the manual Off Mesh Links
      4. Summary
    12. 5. Flocks and Crowds
      1. Learning the origins of flocks
      2. Understanding the concepts behind flocks and crowds
      3. Flocking using Unity's samples
        1. Mimicking individual behavior
        2. Creating the controller
      4. Using an alternative implementation
        1. Implementing the FlockController
      5. Using crowds
        1. Implementing a simple crowd simulation
        2. Using the CrowdAgent component
        3. Adding some fun obstacles
      6. Summary
    13. 6. Behavior Trees
      1. Learning the basics of behavior trees
        1. Understanding different node types
          1. Defining composite nodes
          2. Understanding decorator nodes
          3. Describing the leaf node
      2. Evaluating the existing solutions
      3. Implementing a basic behavior tree framework
        1. Implementing a base Node class
        2. Extending nodes to selectors
        3. Moving on to sequences
        4. Implementing a decorator as an inverter
        5. Creating a generic action node
      4. Testing our framework
        1. Planning ahead
        2. Examining our scene setup
        3. Exploring the MathTree code
        4. Executing the test
      5. Summary
    14. 7. Using Fuzzy Logic to Make Your AI Seem Alive
      1. Defining fuzzy logic
        1. Picking fuzzy systems over binary systems
      2. Using fuzzy logic
        1. Implementing a simple fuzzy logic system
        2. Expanding the sets
        3. Defuzzifying the data
      3. Using the resulting crisp data
        1. Using a simpler approach
      4. Finding other uses for fuzzy logic
        1. Merging with other concepts
        2. Creating a truly unique experience
      5. Summary
    15. 8. How It All Comes Together
      1. Setting up the rules
      2. Creating the towers
        1. Making the towers shoot
      3. Setting up the tank
      4. Setting up the environment
      5. Testing the example
      6. Summary
    16. Index