Reader small image

You're reading from  Unity AI Programming Essentials

Product typeBook
Published inDec 2014
Publisher
ISBN-139781783553556
Edition1st Edition
Tools
Right arrow
Author (1)
Curtis Bennett
Curtis Bennett
author image
Curtis Bennett

Curtis Bennett has been a developer in the games and computer graphics industry for several years. He has worked on developing immersive virtual environments, published research in visual simulation, taught college courses in game development, and worked for various game studios, and he was also an engineer on early versions of the RAIN AI plugin for Unity. Currently, he is the Technical Director for Creative Services at Ideum, which focuses on creating interactive media projects.
Read more about Curtis Bennett

Right arrow

Chapter 3. Behavior Trees

When creating AI for game characters, we want them to appear to behave in realistic ways. This is done by defining different behaviors that a character can do, such as walking, patrolling, attacking, or searching for something, as well as how the character reacts to different items or events in the game environment. In addition to defining a character's behaviors, we need to define when the different behaviors occur. For example, instead of just following a path, we might want the character to change behaviors at different times. This chapter will look at the most popular way to define behaviors and when they occur: behavior trees. We have already looked at behavior trees in the previous chapters, but here, we will go into more detail.

In this chapter, we will learn about:

  • How behavior trees work

  • Implementing complex behavior trees

  • RAIN's behavior trees and the different options that we have to configure them

  • Setting up more advanced behavior trees with a character...

An overview of behavior trees


For game AI, we need to define logic for the different AI entity characters in the game, that is, how they will act and react to different things in the game environment. The traditional and simpler way to do this is to use Finite State Machines (FSMs). In this approach, each character can be in a distinct state, and an FSM is a graph that defines states (nodes) and their transitions (edges). A simple example would be an enemy entity with two states, patrol and attack. The FSM will start in a patrol state, and when it gets close to a player, it transitions to an attack state. FSMs work for very simple state setups such as this, but they don't scale well, as the states and transitions have to be manually configured, usually through code. What if instead of the two states, our enemy character was more realistic and had 10 or even 100 different states, with many transitions between each? This becomes very difficult to manage and implement.

The popular alternative...

RAIN node types


For the RAIN implementation of behavior trees, the behavior nodes are split into two categories: decisions and actions. Actions tell the AI system to actually do something; it is where the actual work of the AI is done. The most common action is the one we saw in the previous chapters, move, which tells the AI system to move a character. Besides move, here is a list of the current actions RAIN supports:

  • The Choose patrol path and Choose path waypoints: These nodes help to move the AI through a network of waypoints.

  • Detect: This finds other AI entities and areas marked in a scene. This node will be covered in Chapter 6, Sensors and Activities.

  • The Evaluate expression: This node evaluates some logic, using RAIN's custom logic system. We will be discussing this node more in this chapter.

  • Animate and Mechanism: These animation nodes manage different animations playing on the entity. We'll look at this node type more in Chapter 10, Animation and AI.

  • Play audio: Plays an audio...

The behavior tree demo


Now that we know about the different nodes we can use, we'll create a demo that shows how to use the action and decision nodes. The demo will show how to have a character perform multiple tasks. We will have an entity, an enemy spaceship, patrolling an area, but only for a given amount of time; then, the ship will return to its home base. The steps for this example might seem overly complicated and we could do a similar AI ourselves without behavior trees with a simple script by hardcoding the different states. However, remember that behavior trees are easily extendible and scalable. With this demo, instead of two behaviors, we could take time to create a more complex character, going up to about 30 behaviors easily, but extending a script to do that would be pretty complicated and hard to maintain.

The start of this is similar to the pathfinding and patrol RAIN demos, except we will use a spaceship model instead of a walking character. You'll need to create a simple...

Summary


In this chapter, we went through the most popular way to set up behaviors for game entities and behavior trees. We went through the process of defining behaviors, deciding the different actions the behaviors will perform and the transitions between the actions. Then, we set up a character and run the game. This is the process to create logic for your game characters, deciding what the behaviors are and the different conditions that can cause them to become active.

In the next chapter, we will look at how to use behavior trees more with character movement and see how to set up the wander behavior for crowd creation. We will explore AIs that will control a large collection of NPCs moving in distinctly separate low-repeating paths.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity AI Programming Essentials
Published in: Dec 2014Publisher: ISBN-13: 9781783553556
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
Curtis Bennett

Curtis Bennett has been a developer in the games and computer graphics industry for several years. He has worked on developing immersive virtual environments, published research in visual simulation, taught college courses in game development, and worked for various game studios, and he was also an engineer on early versions of the RAIN AI plugin for Unity. Currently, he is the Technical Director for Creative Services at Ideum, which focuses on creating interactive media projects.
Read more about Curtis Bennett