Version This package was build using unity 2022.2, but also confirmed to be working with 2021.3.
Overview
Below is a quick overview of the different components of classes and how they are connected to an Action.
Setup in Unity
Create a class called WanderGoal that extends GoalBase.
WanderGoal.cs
using CrashKonijn.Goap.Behaviours;
public class WanderGoal : GoalBase
{
}
Create a class called WanderAction that extends ActionBase. The generic value of the class is the type of the data class used in this goal.
WanderAction.cs
using CrashKonijn.Goap.Behaviours;
using CrashKonijn.Goap.Classes;
using CrashKonijn.Goap.Enums;
using CrashKonijn.Goap.Interfaces;
using UnityEngine;
public class WanderAction : ActionBase<WanderAction.Data>
{
// Called when the class is created.
public override void Created()
{
}
// Called when the action is started for a specific agent.
public override void Start(IMonoAgent agent, Data data)
{
// When the agent is at the target, wait a random amount of time before moving again.
data.Timer = Random.Range(0.3f, 1f);
}
// Called each frame when the action needs to be performed. It is only called when the agent is in range of it's target.
public override ActionRunState Perform(IMonoAgent agent, Data data, ActionContext context)
{
// Update timer.
data.Timer -= context.DeltaTime;
// If the timer is still higher than 0, continue next frame.
if (data.Timer > 0)
return ActionRunState.Continue;
// This action is done, return stop. This will trigger the resolver for a new action.
return ActionRunState.Stop;
}
// Called when the action is ended for a specific agent.
public override void End(IMonoAgent agent, Data data)
{
}
public class Data : IActionData
{
public ITarget Target { get; set; }
public float Timer { get; set; }
}
}
Create a class called WanderTargetSensor that extends LocalTargetSensorBase. The generic value of the class is the type of the data class used in this goal.
WanderTargetSensor.cs
using CrashKonijn.Goap.Classes;
using CrashKonijn.Goap.Interfaces;
using CrashKonijn.Goap.Sensors;
using UnityEngine;
public class WanderTargetSensor : LocalTargetSensorBase
{
// Called when the class is created.
public override void Created()
{
}
// Called each frame. This can be used to gather data from the world before the sense method is called.
// This can be used to gather 'base data' that is the same for all agents, and otherwise would be performed multiple times during the Sense method.
public override void Update()
{
}
// Called when the sensor needs to sense a target for a specific agent.
public override ITarget Sense(IMonoAgent agent, IComponentReference references)
{
var random = this.GetRandomPosition(agent);
return new PositionTarget(random);
}
private Vector3 GetRandomPosition(IMonoAgent agent)
{
var random = Random.insideUnitCircle * 5f;
var position = agent.transform.position + new Vector3(random.x, 0f, random.y);
return position;
}
}
Create a class called AgentMoveBehaviour. This class will be called by the AgentBehaviour to move the agent to a target.