Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Unity 2017 Game AI Programming - Third Edition - Third Edition

You're reading from  Unity 2017 Game AI Programming - Third Edition - Third Edition

Product type Book
Published in Jan 2018
Publisher Packt
ISBN-13 9781788477901
Pages 254 pages
Edition 3rd Edition
Languages

Table of Contents (10) Chapters

Preface 1. The Basics of AI in Games 2. Finite State Machines and You 3. Implementing Sensors 4. Finding Your Way 5. Flocks and Crowds 6. Behavior Trees 7. Using Fuzzy Logic to Make Your AI Seem Alive 8. How It All Comes Together 9. Other Books You May Enjoy

Flocks and Crowds

Flocks and crowds are two additional core AI concepts we'll be exploring in this book. As you'll see in this chapter, flocks are relatively simple to implement, and they add a fairly extraordinary amount of realism to your simulation in just a few lines of code. Crowds can be a bit more complex, but we'll be exploring some of the powerful tools that come bundled with Unity to get the job done. In this chapter, we'll cover the following topics:

  • Learning the history of flocks and herds
  • Understanding the concepts behind flocks
  • Flocking using the traditional algorithm
  • Using realistic crowds

Technical Requirements

Learning the origins of flocks

The flocking algorithm dates all the way back to the mid-80s. It was first developed by Craig Reynolds, who developed it for use in films, the most famous adaptation of the technology being the swarm of bats in Batman Returns in 1992, for which he won an Oscar. Since then, the use of the flocking algorithm has expanded beyond the world of film into various fields, from games to scientific research. Despite being relatively efficient and accurate, the algorithm is also very simple to understand and implement.

Understanding the concepts behind flocks and crowds

As with previous concepts, it's easiest to understand flocks and herds by relating them to the real-life behaviors they model. As simple as it sounds, these concepts describe a group of objects, or boids as they are called in artificial intelligence lingo, moving together as a group. The flocking algorithm gets its name from the behavior birds exhibit in nature, where a group of birds follow one another toward a common destination, mostly keeping a fixed distance from each other. The emphasis here is on the group. We've explored how single agents can move and make decisions on their own, but flocks are a relatively computationally efficient way of simulating large groups of agents moving in unison while modeling unique movement in each boid that doesn't rely on randomness or predefined paths.

The implementation...

Using the Reynolds algorithm

Without further ado, let's dive into the Reynolds flocking algorithm. There are two main scripts for our flocking implementation: Boid.cs and FlockController.cs. The sample code for this chapter provides a scene with all the necessary setup for testing. You'll also notice a third script named TargetMovement.cs, which we use to move a target that our flock will follow around the scene.

For our boid, we can use a simple cube as a prefab. Of course, feel free to replace the cube with any art you want. Let's add the Boid.cs script to our boid prefab. The code looks like this:

using UnityEngine;

public class Boid : MonoBehaviour
{
[SerializeField]
private FlockController flockController;

//The modified direction for the boid.
private Vector3 targetDirection;
//The Boid's current direction.
private Vector3 direction...

Using crowds

Crowd simulations are far less cut-and-dried. There really isn't any one way to implement them in a general sense. While not a strict definition, the term "crowd simulation" generally refers to simulating crowds of humanoid agents navigating an area while avoiding each other and the environment. Like flocks, the use of crowd simulations has been widely used in films. For example, the epic armies of Rohan, Gondor, and Mordor battling one another in The Lord of the Rings were completely procedurally generated using the crowd simulation software Massive, which was created for use in the film. While the use of crowd algorithms is not as widespread in video games as in films, certain genres rely on the concept more than others. Real-time strategy games often involve armies of characters moving in unison across the screen, and many sandbox games simulate...

Summary

In this chapter, we learned how to implement a flocking behavior system. We implemented it using custom direction vectors to control the boids' movement that we calculated by applying Craig Reynolds' three main flocking concepts—alignment, coherence, and separation. We then applied our flocking behavior to the flying objects, but you can apply the techniques in these examples to implement other character behaviors, such as fish shoaling, insects swarming, or land animals herding. You'll only have to implement different leader movement behaviors, such as limiting movement along the y axis for characters that can't move up and down. For a 2D game, we would just freeze the y position. For 2D movement along uneven terrain, we would have to modify our script to not put any forces in the y direction.

We also took a look at crowd simulation and even...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Unity 2017 Game AI Programming - Third Edition - Third Edition
Published in: Jan 2018 Publisher: Packt ISBN-13: 9781788477901
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.
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}