The Fame Crowd Simulation API

The Fame Crowd Simulation API by TechBizXccelerator is available at Unity Asset Store under the name Crowd Simulator API for $45 at the time of writing this book. It allows you to create groups and customize steering behaviors. For our crowd demo, we will create a demo with many spaceships traveling in a group.

Setting up a scene with Fame

To create our demo, first create a new scene with a plane as the ground. Like most of the AI plugins in this book, Fame Crowd Simulation also supports Unity's built-in terrain system, but for this demo, a basic ground plane will be fine. Fame also uses a singleton pattern that has one class that manages everything for crowd management, so create an empty GameObject and call it Crowd Manager. Then, import Fame if it is not already in your project, and attach the FameManager script from Fame Assets/FameScripts/FameManager.cs to the Crowd Manager GameObject. This initial setup is shown in the following screenshot:

Setting up a scene with Fame

The following should be the hierarchy:

Setting up a scene with Fame

This is our basic Fame scene setup with a Crowd Manager object.

The FameManager object will be our main point of interaction with Fame, and we can interact with it from anywhere in our game code.

Unlike the other systems we saw where we create the AI characters individually to define our crowds, in Fame, we will define a group of characters, also called a flock. Add the ship model from Chapter 3, Behavior Trees, and then create another empty GameObject and call it Ships. Then, attach the FlockGroup.cs script to it from Assets/FrameScripts.

Note

To avoid the warning about not having a terrain in FAME settings, disable Enable Terrain Effect in FameManager and Get Info From Terrain GameObject in Fame terrain.

FlockGroup is the main class to hold a group of characters, and it has several settings we can customize:

  • Flock Type: This is Ground or Air and defines whether the AI characters will move on a plane (ground) or move in all three axes (air). If set to Air, characters will ignore any terrain. You might think we would set our ships to air, but we want them to all hover at the same height, so keep this set to ground.
  • Num Agent: This is the number of agents (or AI characters) in the group. This can be a very high number as Fame is efficient, but for our demo, we will set this to 8.
  • Avatar: This is the GameObject set to be the individual members of the crowd. For our demo, this is the ship model.
  • FlockMemberScript: You can create a custom script to define how the members of your crowd will act. For this demo, we will keep things simple and use Fame's default FlockMemberScript.cs script.

The following screenshot shows our Fame FlockGroup settings for the ship group:

Setting up a scene with Fame

Setting up a group

Next, we need to create the initial formation we want our group to be in. With the Ships GameObject selected, you will see three connected gizmos in the Unity 3D view that represent the shape of the initial group. Go to Formation Shape in the Inspector panel for Ships and click on Add Point. This creates a fourth point for the shape of the group. Arrange the points into a pyramid-like shape and click on Create Avatars. A set of ships in a group will be created:

Setting up a group

This is our group setup that shows the control points that define the boundary for our group. You can see our ship group shape with eight ships has been created.

Creating a group object was really easy with Fame. Now, we need to set up a way for our crowd to move. We will have the ships move across the plane to a target point. Set the Z scale of the plane to a larger value, create a sphere object to be our target position, and place it across the plane. The screen should look as follows:

Setting up a group

You can see a group of ships with a target placed.

When we send the group, or flock, the command to move to the target location, it will move the position of the flock to match it, which isn't necessarily the center of the flock. In this demo, the center of the flock is the upper-left corner from the camera view, which is why the target sphere is offset to the upper-left corner.

Now that we have a target element in the scene, we need to give Fame a command to move the group to it. Fame supports path following, but as most of the time we will want our groups to move dynamically, such as chasing a player, we will look at how to move the crowd by code. We have a lot of ways to set up a script to control the flock, but we'll take the simplest route and use a game manager. Create another empty GameObject and call it GameManager. Then, create a new script, GameManager.cs, and attach it to the GameManager object.

Add the following code to it:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

  public Transform target;

  void Start () {

  }

  void Update () {
    FlockGroup group = FameManager.GetFlockGroup(1); // Get first flock

    group.MoveGroupAbs(target.position);
  }
}

In this code, we have a public variable for the target for the flock. In the Unity editor, drag the Sphere target object to this field to set its value. Then, in the update, we just get the flock group from Fame manager (which is a singleton class with static methods, so we don't need a reference to it). Then, we just use MoveGroupAbs that tells the group to move to a specific location. (There is also a method called MoveGroup that takes in an offset instead of an absolute position you can use if you want the crowd to move in a general direction instead of to a specific point.) If you run the demo now, the ships will travel down the ground to the target point. As you can see, setting up a crowd and giving directions for it to move is very easy and straightforward with Fame.

Adding obstacles to Fame

Next, let's add an obstacle inbetween the ships and their target. Increase the size of the ground plane by increasing its X scale. Then, add a cylinder to the middle of the scene to be an obstacle for the ships. To have this obstacle recognized by Fame, add the Fame obstacle script, FameObstacle.cs, to it. Fame allows two types of obstacles: round (circles) and 2D polygons. You specify the polygon ones, the same as we did for group shapes, by modifying control points. For our obstacle, we just need it to match the radius of the cylinder. In this example, the cylinder has a scale of 50, which makes its radius 25, so set the obstacle's radius to 25.

The demo should now look like the following screenshot:

Adding obstacles to Fame

This is the obstacle setup for our ship group.

If you run the demo now, the ships will go to their target and smoothly avoid the obstacle. You can see in the next screenshot how the ships avoid our obstacle:

Adding obstacles to Fame

Adding vector fields to Fame

We just saw how to add obstacles to our Fame crowd scene. Right now, all of our ships split when avoiding the cylinder, about half to the left and the other half to the right. However, we want all of them to move to the left. To do this, we can vector fields to the scene. Vector fields are a popular way of adding directional hints to a scene, and they are defined areas that have directional vectors that are associated with them. When a character is inside the field, the vector helps move the character in the desired direction. Vector fields can be a powerful tool for level design and are easy to add to your scene. To see how easy it is to add them to the scene, add a new empty GameObject to the scene and name it Field. Then, attach the FameField.cs script from Assets/Fame Assets/FameScripts to it. The field can be rectangular, called Uniform or Circular. Select Uniform for the type and set x and z widths to 75. Then, set the angle to 45 and the magnitude to 100. The white arrow visualizes the angle for the vector field. Place the field over the initial positions for the ships, as shown:

Adding vector fields to Fame

If you run the demo now, ships will all veer to the left before avoiding the cylinder. For larger levels, you can set up multiple vector fields; they are a good way to control the general movement of AI characters.

This Fame demo showed you the basics you'll need to create organized crowds for most games: creating crowds, giving them directions to move, and adding obstacles and vector fields. Next, we will look at another specialized AI plugin, ANT-Op. You can see the ship group changing its direction because of the vector field. You can also notice how our crowd uses the vector fields to direct ship movement to the left:

Adding vector fields to Fame
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.222.239