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 4. Crowd Chaos

Part of having a realistic game environment is having the nonplayer characters and NPCs act in a believable way. Crowd chaos is all about keeping NPCs busy to create crowded backgrounds for our games. Perhaps your game is set up in a mall, or a city, or any other place where lots of NPCs need to wander around and look like they are doing something. Crowds like these will be the subject of this chapter and the next.

In this chapter, you will learn about:

  • Working with crowd chaos

  • How to create crowd type characters in the React and RAIN AI packages

  • Expanding our knowledge of behavior trees

An overview of crowd chaos


Crowd Chaos is all about giving separate interests to a large number of NPCs, so they look like they are living their own lives. In its lightest form, this can be something very simple, such as a whole bunch of NPCs picking random targets, walking to them, possibly sitting still for a moment, and then starting over. This stands out in real-time strategy games when buildings are constructed, and you see a construction worker walking to random points of the structure and waving their arms about.

Every game that needs crowd chaos will typically have a basic wandering base, and it can be extended as needed. Perhaps the crowd will form lines of more NPCs that are waiting at a spot. Perhaps the targets have changing values and AIs prefer higher values. They pick up a random block and put it somewhere else. The base wandering behavior needed for these and other crowd behaviors is what we will implement in both React and RAIN AI.

React AI


For this demo, we will duplicate the path-following behavior demo in React from Chapter 1, Pathfinding, and then update it to see some emergent behavior develop from it. We will need to complete the following:

  • Create a world with some walls

  • Create target markers in the scene

  • Create a script with a custom editor to find the targets

  • Create the behavior

  • Create NPCs and assign the behavior

Setting up a scene with React

To start out with, we will need a basic environment for characters to walk in. Create a plane, call it Floor, and add some cubes, shaping them into walls. These will need to be static so that Unity's navigation mesh can find them. Then, we'll need to select the floor and add the navigation mesh. If you've forgotten how to do any of this, it is all covered in the React tutorial in Chapter 1, Pathfinding.

Next, we need some targets. We'll use a different approach for this from our previous demos and let GameObjects mark the targets. Create an empty GameObject and call it Targets...

RAIN AI


We have already looked at a basic wander behavior for RAIN in Chapter 2, Patrolling, when creating patrolling AI, but there, we manually created each possible location for the NPC to go to. In this demo, we will pick random points to wander to from anywhere in the navigation mesh. The NPCs won't have any interaction, though such features are not difficult to add. Here is a breakdown of the steps we will do in this section:

  • Set up a world

  • Build the behavior tree

  • Add a script to pick new points

  • Add the NPCs

  • Learn about the RAIN AI world and behavior tree setup

First, we'll create a new world. Start with a large plane called floor, and add some cubes shaped into walls. You will need to add a navigation mesh and bake it into the scene. These are the same steps we have performed for RAIN demos in earlier chapters. The following is an example of how the scene could look:

Next comes the behavior tree. From the RAIN menu, select Behavior Tree Editor. Create a new tree called RandomWalk. The objective...

Summary


We were able to use scripting and behavior trees in both React AI and RAIN to effectively create a wandering AI. Each AI had strengths and weaknesses, though the weaknesses were more of a preference.

Behavior tree editors were used in both RAIN and React, and both work in a similar fashion. In RAIN, you can start editing a tree from the menu, or from the editor itself. (It had the option to select the behavior directly in the editor.) With React, you can do this from the Project tab, by right-clicking and choosing to edit it. React had premade scripts that can do nearly all the actions that were needed, except that instead of selecting randomly from a list of targets with the tag, it would select a target expecting only one object with that tag. With RAIN, we made a custom action node to choose a location to go to.

Both React and RAIN AI are general AI systems that are useful for many different types of game situations, so neither were designed specifically to handle crowds. In the...

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