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
usingCrashKonijn.Goap.Behaviours;usingCrashKonijn.Goap.Classes;usingCrashKonijn.Goap.Enums;usingCrashKonijn.Goap.Interfaces;usingUnityEngine;publicclassWanderAction:ActionBase<WanderAction.Data>{ // Called when the class is created.publicoverridevoidCreated() { } // Called when the action is started for a specific agent.publicoverridevoidStart(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.
publicoverrideActionRunStatePerform(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)returnActionRunState.Continue; // This action is done, return stop. This will trigger the resolver for a new action.returnActionRunState.Stop; } // Called when the action is ended for a specific agent.publicoverridevoidEnd(IMonoAgent agent,Data data) { }publicclassData:IActionData {publicITarget Target { get; set; }publicfloat 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
usingCrashKonijn.Goap.Classes;usingCrashKonijn.Goap.Interfaces;usingCrashKonijn.Goap.Sensors;usingUnityEngine;publicclassWanderTargetSensor:LocalTargetSensorBase{ // Called when the class is created.publicoverridevoidCreated() { } // 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.
publicoverridevoidUpdate() { } // Called when the sensor needs to sense a target for a specific agent.publicoverrideITargetSense(IMonoAgent agent,IComponentReference references) {var random =this.GetRandomPosition(agent);returnnewPositionTarget(random); }privateVector3GetRandomPosition(IMonoAgent agent) {var random =Random.insideUnitCircle*5f;var position =agent.transform.position+newVector3(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.