Comment on page
Sensors
Sensors help the GOAP system understand the current game situation.
There are two main types of sensors:
WorldSensor
and TargetSensor
.Sensors can work in two modes:
Global
or Local
.- Global: These sensors give information for all agents. For instance,
IsDaytimeSensor
checks if it's day or night for everyone. - Local: These sensors check only when the
Planner
runs. They give information for just one agent. For example,ClosestAppleSensor
finds the nearest apple for a specific agent.
WorldSensor
checks the game's situation for an agent. It uses WorldKey
to show each situation. The Planner
uses this to pick the best action.Examples:
IsHungrySensor
checks if the agent is hungry.HasAppleSensor
checks if the agent has an apple.
To create a new
WorldSensor
, create a new class that inherits from LocalWorldSensorBase
or GlobalWorldSensorBase
and implement its Sense
method.IsHungrySensor.cs
1
using CrashKonijn.Goap.Behaviours;
2
using CrashKonijn.Goap.Classes;
3
using CrashKonijn.Goap.Classes.References;
4
using CrashKonijn.Goap.Sensors;
5
using Demos.Shared.Behaviours;
6
7
namespace Demos.Simple.Sensors.World
8
{
9
public class IsHungrySensor : LocalWorldSensorBase
10
{
11
public override void Created()
12
{
13
}
14
15
public override void Update()
16
{
17
}
18
19
public override SenseValue Sense(IMonoAgent agent, IComponentReference references)
20
{
21
// References are cached by the agent.
22
var hungerBehaviour = references.GetComponent<HungerBehaviour>();
23
24
if (hungerBehaviour == null)
25
return false;
26
27
return hungerBehaviour.hunger > 20;
28
}
29
}
30
}
TargetSensor
finds a position for a TargetKey
. The Planner
uses this to know how far actions are.There are two kinds of
Target
: TransformTarget
and PositionTarget
.- TransformTarget: Use this when the target can move. For example,
ClosestEnemySensor
finds a moving enemy. - PositionTarget: Use this for a fixed spot. Like,
WanderTargetSensor
finds a random spot that doesn't move.
To create a new
TargetSensor
, create a new class that inherits from LocalTargetSensorBase
or GlobalTargetSensorBase
and implement its Sense
method.ClosestAppleSensor.cs
1
using CrashKonijn.Goap.Behaviours;
2
using CrashKonijn.Goap.Classes;
3
using CrashKonijn.Goap.Classes.References;
4
using CrashKonijn.Goap.Interfaces;
5
using CrashKonijn.Goap.Sensors;
6
using Demos.Simple.Behaviours;
7
using UnityEngine;
8
9
namespace Demos.Simple.Sensors.Target
10
{
11
public class ClosestAppleSensor : LocalTargetSensorBase
12
{
13
private AppleCollection apples;
14
15
public override void Created()
16
{
17
this.apples = GameObject.FindObjectOfType<AppleCollection>();
18
}
19
20
public override void Update()
21
{
22
}
23
24
public override ITarget Sense(IMonoAgent agent, IComponentReference references)
25
{
26
var closestApple = this.apples.Get().Closest(agent.transform.position);
27
28
if (closestApple is null)
29
return null;
30
31
return new TransformTarget(closestApple.transform);
32
}
33
}
34
}
WanderTargetSensor.cs
1
using CrashKonijn.Goap.Behaviours;
2
using CrashKonijn.Goap.Classes;
3
using CrashKonijn.Goap.Classes.References;
4
using CrashKonijn.Goap.Interfaces;
5
using CrashKonijn.Goap.Sensors;
6
using UnityEngine;
7
8
namespace Demos.Simple.Sensors.Target
9
{
10
public class WanderTargetSensor : LocalTargetSensorBase
11
{
12
private static readonly Vector2 Bounds = new Vector2(15, 8);
13
14
public override void Created()
15
{
16
}
17
18
public override void Update()
19
{
20
}
21
22
public override ITarget Sense(IMonoAgent agent, IComponentReference references)
23
{
24
var random = this.GetRandomPosition(agent);
25
26
return new PositionTarget(random);
27
}
28
29
private Vector3 GetRandomPosition(IMonoAgent agent)
30
{
31
var random = Random.insideUnitCircle * 5f;
32
var position = agent.transform.position + new Vector3(random.x, 0f, random.y);
33
34
if (position.x > -Bounds.x && position.x < Bounds.x && position.z > -Bounds.y && position.z < Bounds.y)
35
return position;
36
37
return this.GetRandomPosition(agent);
38
}
39
}
40
}
Last modified 1mo ago