Using senses with RAIN

For this demo, we will use RAIN 2.14 and have a ship that patrols a path, looks for pieces of gold, and picks them up. To start, we'll use a setup similar to that of the demo in Chapter 3, Behavior Trees. You can start from there or recreate it; we just need a ship, a wall, a path, with the ground being a little larger, and the objects spread out a little.

Note

When changing the base geometry of your game levels, you need to regenerate the navigation mesh. This is done by selecting the Navigation Mesh object in your scene and clicking on the Generate NavMesh button.

Here is our basic setup. The following image shows the starting point of our sensor demo:

Using senses with RAIN

We also just need the behavior tree for the ship to only patrol the path. Set up this behavior like we did in Chapter 2, Patrolling, or if you are using the behavior tree demo, delete the timer node functionality. The new behavior tree should look like the following screenshot:

Using senses with RAIN

This will be the starting point of the behavior tree for our sensor demo. If you start the demo now, the ship will just keep circling the wall.

Setting up aspects in RAIN

For our sensor demo, we will have the ship look for gold, which will be represented by a simple game object. Create a Sphere object in Unity by navigating to Game Object | Create Other | Sphere. Make it a little smaller by giving it Scale of 0.25 for X, Y, and Z, and change the material to a golden color. We'll be duplicating the object later so if you want duplicating to be easier, make it a prefab. This is our starting point, as illustrated in the following screenshot:

Setting up aspects in RAIN

The starting point of our object (Sphere)

To have an aspect, the game object needs an Entity component. With Gold selected, go to RAIN | Create Entity. There are a few settings to customize, but for now just change the Entity Name field to Gold. The other important setting is Form, which is the game object attached to it; we can leave it to Sphere.

Click on the Add Aspect dropdown and select Visual Aspect. Set the aspect name to Gold as well. The setting should look like the following screenshot:

Setting up aspects in RAIN

We now have an entity with a visual aspect. Create a Prefab tab for this Gold object and then add it to the opposite side of the wall as the ship. The scene should look like the following screenshot:

Setting up aspects in RAIN

A sensor demo with gold

Setting up a visual sensor in RAIN

We have the gold aspect; next we need a visual sensor. Select Ship AI, click on the eye icon for the sensors tab, and from the Add Sensor dropdown, select Visual Sensor. Go to the Advanced Settings (selecting the gear icon) icon and adjust the horizontal and vertical angles as well as the range until the sensor can see a bit in front of the ship. Typically, you will make these very large so that the character can see most of the level. For this demo, the sensor values are 120 for Horizontal Angle, 45 for Vertical Angle, and 15 for Range. Also, check the Require Line of Sight option so that the ship can't see gold through the wall. The setup should look like the following screenshot:

Setting up a visual sensor in RAIN

If you run the demo now, you will see the ship moving with the sensor (in Editor View). The ship with a visual sensor should look like the following image:

Setting up a visual sensor in RAIN

This completes setting up the sensor for our ship.

Changing activities based on sensing

We now have the ship sensing the gold as it passes by, but it still doesn't react to it. To do this, we will update the behavior tree for the ship.

The first thing we want is a detect node as the ship is moving so it can know if it sees Gold. Open the behavior tree for the ship and create a detect node. As the detect node will be running continuously, change its Repeat type to Forever and right-click on the root node and change its type to Parallel. For the detection part of the detect node to work, set the Aspect field to "Gold" and set the sensor it will be using to "Visual Sensor". Finally, we need to set the form of the aspect, the game object attached. Set Form Variable to gold.

Note

The whole quotes thing in RAIN can be confusing: why some fields need quotes and others don't. This is planned to be improved in future versions of RAIN, but for now for Expressions (fields with the little e symbol) a value with quotes means the name of an object and without means the value of a variable. So in our case, "Visual Sensor" and "Gold" were both in quotes as they were referring to objects by name, but gold is an actual variable we store data in, so it doesn't have quotes.

Your setup should look like the following screenshot:

Changing activities based on sensing

In the preceding screenshot, you can see the behavior tree with the detect node.

Now if you run the game, the gold will be detected, but the ship still doesn't move to it yet. To do this, we will use a selector node similar to the original behavior tree demo. Place a selector node under root and create a constraint node as a child with a Constraint value of gold == null. Then, move the original patrol node to be a child of the constraint. The setup should look the following screenshot:

Changing activities based on sensing

The preceding screenshot shows a detection behavior tree with a constraint node.

Now if you run the demo, when the ship sees the gold, the gold value will not be null and it will stop moving. However, instead of stopping, we want it to move over to the gold; so, add another constraint node with the Expressiongold != null value and a move node below it that has a Move Target value of gold. Here is how the behavior tree with the detect node settings will look:

Changing activities based on sensing

If you run the demo now, the ship will move to the gold when it sees it. However, let's change this so that the ship goes back to patrolling after the pickup. Make sure that both root and selector nodes are set to Forever for their Repeat type. Then, create a new custom action node (like in Chapter 3, Behavior Trees) and put it under the move node for the gold. Create a new class for the custom action and call it PickUpGold. Set its code to this:

using UnityEngine;
using RAIN.Core;
using RAIN.Action;

[RAINAction]
public class PickUpGold : RAINAction
{
    public PickUpGold()
    {
        actionName = "PickUpGold";
    }

    public override void Start(AI ai)
    {
        base.Start(ai);

        GameObject gold = ai.WorkingMemory.GetItem<GameObject>("gold");

        ai.WorkingMemory.SetItem<GameObject>("gold", null);

        Object.Destroy(gold);
    }

    public override ActionResult Execute(AI ai)
    {
        return ActionResult.SUCCESS;
    }

    public override void Stop(AI ai)
    {
        base.Stop(ai);
    }
}

The important code here is in the Start method. We got the gold game object that was sensed from the memory and then erased it from memory by setting the gold value to null. Then, we destroyed the gold object, so it won't be sensed anymore. If you run the code now, the ship will follow the path, pick up gold when it sees it, and then go back to the path.

Next, try adding several more gold prefabs to the scene and run the demo, as shown in the following image:

Changing activities based on sensing

Now in the demo, the ship will go and collect all the different gold pieces it sees and then return to the path.

RAIN sensor filters

If you tried running the demo with multiple gold pieces, you must have seen a small problem. The ship always goes to the first piece of gold it sees, but that might not be the closest. If it sees a distant piece from the corner of its eye, it will go straight to it even if there are ones closer to it. A quick fix for this is to add a filter to the RAIN sensors. Filters are ways to manipulate the list of sensed objects, and RAIN might have more in the future but for now, it just has one: NearestXFilter. Select Visual Sensor in the ship and set the Size field to 1 and select NearestXFilter under the Filters section. The following screenshot will show the settings of NearestXFilter on the sensor:

RAIN sensor filters

The NearestXFilter filter will send a given number of closest objects to the sensor. In our case, we just leave it to one. If you run the demo now, the ship will always pick up the gold that it can see and that is the closest to it first. This completes our ship demo.

..................Content has been hidden....................

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