Sensors are used to provide values for WorldKey or TargetKey types by reading the current state of the world and providing it to the WorldState.
The base class you must extend depends on the Type of key and the Scope of the sensor:
| Key Type | Local Scope (Single Agent) | Global Scope (All Agents) |
|---|
| WorldKey | LocalWorldSensorBase | GlobalWorldSensorBase |
| TargetKey | LocalTargetSensorBase | GlobalTargetSensorBase |
Sensor Lifecycle Methods:
Created(): Called when the sensor is initialized.Update(): Called every frame an agent using this sensor needs it. Useful for caching data (e.g., finding the closest tree).Sense(IActionReceiver agent, IComponentReference references, ITarget existingTarget): The core logic that returns the sensed value (e.g., a PositionTarget).
using CrashKonijn.Agent.Core;
using CrashKonijn.Goap.Runtime;
using UnityEngine;
namespace CrashKonijn.Docs.GettingStarted.Sensors
{
[GoapId("IdleTargetSensor-c34e9575-d171-4044-9b83-a91a1c32e214")]
public class IdleTargetSensor : LocalTargetSensorBase
{
public override void Created() { }
public override void Update() { }
public override ITarget Sense(IActionReceiver agent, IComponentReference references, ITarget existingTarget)
{
var random = this.GetRandomPosition(agent);
if (existingTarget is PositionTarget positionTarget)
{
return positionTarget.SetPosition(random);
}
return new PositionTarget(random);
}
private Vector3 GetRandomPosition(IActionReceiver agent)
{
// Implementation logic...
return Vector3.zero;
}
}
}