3. Pears

Pears

In this part of the tutorial we will add a PickupPearGoal and let the agent pickup pears if it comes close to them.

Creating the classes

  1. Let's start by boiler plating the files that we need. Use the generator to create the following files:

    • Goals: PickupPearGoal, EatGoal

    • Actions: PickupPearAction, EatAction

    • WorldKeys: PearCount, Hunger

    • TargetKeys: ClosestPear

  2. We will implement the classes in a bit, first let's make sure that we know what pears are. Create a new class called PearBehaviour in the Behaviours folder and add the following code:

PearBehaviour.cs
using UnityEngine;

namespace CrashKonijn.Docs.GettingStarted.Behaviours
{
    public class PearBehaviour : MonoBehaviour
    {
    }
}
  1. In our scene, create a new GameObject using GameObject > 3D Object > Sphere. Rename the object to Pear and add the PearBehaviour component to it. Pears are generally smaller than agents, so let's adjust the scale to 0.5 on all axes. You can remove the collider as we won't need it.

  2. Let's create a new material for the pear. Right-click in the Assets folder and select Create > Material. Rename the material to PearMaterial and change the color to a nice yellow/green. Drag the material onto the Pear object.

  3. In the GettingStarted folder let's create a new folder called Prefabs. Drag the Pear object into this folder to create a prefab.

  4. Let's duplicate the pear a couple of times in the scene and place them in different locations. Please make sure al your pears are on 0 on the y-axis of their positions.

  5. For these actions we need data that represents the PearCount and Hunger values. The source of truth for these values must be our own MonoBehaviours. Let's create a script called DataBehaviour in the Behaviours folder and add the following code:

  1. Add the new DataBehaviour to the Agent object in the scene.

  2. Create the sensors required for the PearCount and ClosestPear keys. This time we'll use something called a MultiSensor. This sensor can provide multiple keys at once. Create a new class called PearSensor in the Sensors folder and inherit from MultiSensorBase.

Editing the PickupPearAction

Let's implement the PickupPearAction so it actually performs the action of picking up a pear.

Configuring the PickupPearGoal

  1. In the Capabilities folder let's create the PearCapability class and add the following code:

  1. Edit the DemoAgentTypeFactory and add the PearCapability to the list of capabilities.

Adding the PickupPearGoal to the brain.

  1. Adjust the RequestGoal call in our BrainBehaviour to include the PickupPearGoal.

  1. Start the scene and watch the agent pickup pears when it comes close enough!

Pears

You graph should now look like this:

New Graph

Adding the EatGoal and EatAction

Adjusting the EatAction

  1. Let's adjust the EatAction to consume the pears that the agent has picked up.

Creating the EatCapability

  1. This is your time to shine, create a new capability called EatCapability that uses the EatGoal and EatAction.

  1. Add the capability to you agent type.

Adjusting the brain

Let's adjust the BrainBehaviour to include the EatGoal when the hunger is higher than 50!

A mixed graph

Play the scene and watch the agent eat the pears when it's hungry!

Your graph should now look like this. As you can see the actions of the different capabilities are mixed into a single graph. The PickupPearAction can now also be performed when the EatGoal is active.

Mixed Graph

Last updated