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
Let's start by boiler plating the files that we need. Use the generator to create the following files:
Goals:
PickupPearGoal,EatGoalActions:
PickupPearAction,EatActionWorldKeys:
PearCount,HungerTargetKeys:
ClosestPear
We will implement the classes in a bit, first let's make sure that we know what pears are. Create a new class called
PearBehaviourin theBehavioursfolder and add the following code:
using UnityEngine;
namespace CrashKonijn.Docs.GettingStarted.Behaviours
{
public class PearBehaviour : MonoBehaviour
{
}
}In our scene, create a new GameObject using
GameObject > 3D Object > Sphere. Rename the object toPearand add thePearBehaviourcomponent to it. Pears are generally smaller than agents, so let's adjust the scale to0.5on all axes. You can remove the collider as we won't need it.Let's create a new material for the pear. Right-click in the
Assetsfolder and selectCreate > Material. Rename the material toPearMaterialand change the color to a nice yellow/green. Drag the material onto thePearobject.In the
GettingStartedfolder let's create a new folder calledPrefabs. Drag thePearobject into this folder to create a prefab.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.
For these actions we need data that represents the
PearCountandHungervalues. The source of truth for these values must be our ownMonoBehaviours. Let's create a script calledDataBehaviourin theBehavioursfolder and add the following code:
Add the new
DataBehaviourto theAgentobject in the scene.Create the sensors required for the
PearCountandClosestPearkeys. This time we'll use something called aMultiSensor. This sensor can provide multiple keys at once. Create a new class calledPearSensorin theSensorsfolder and inherit fromMultiSensorBase.
Editing the PickupPearAction
Let's implement the PickupPearAction so it actually performs the action of picking up a pear.
Configuring the PickupPearGoal
In the
Capabilitiesfolder let's create thePearCapabilityclass and add the following code:
Edit the
DemoAgentTypeFactoryand add thePearCapabilityto the list of capabilities.
In the
Capabilitiesfolder create a newCapabilityConfigasset and name itPearCapability.Add a new
Goal, select thePickupPearGoaland add a condition ofPearCountGreaterThanOrEqual3.Add a new
Action, select thePickupPearActionand add an effect ofPearCountIncrease. Set the target toClosestPear.Add a new MultiSensor and select the
PearSensor.Select the
DemoAgentTypeConfigand add thePearCapabilityto the list of capabilities.
Adding the PickupPearGoal to the brain.
Adjust the
RequestGoalcall in ourBrainBehaviourto include thePickupPearGoal.
Start the scene and watch the agent pickup pears when it comes close enough!

You graph should now look like this:

Adding the EatGoal and EatAction
Adjusting the EatAction
Let's adjust the
EatActionto consume the pears that the agent has picked up.
Creating the EatCapability
This is your time to shine, create a new capability called
EatCapabilitythat uses theEatGoalandEatAction.
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.

Last updated