1
This commit is contained in:
130
Src/UnityPluginsSupport/NodeCanvas/Pathfinding/AIWander.cs
Normal file
130
Src/UnityPluginsSupport/NodeCanvas/Pathfinding/AIWander.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using NodeCanvas.Framework;
|
||||
using ParadoxNotion.Design;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
namespace BITKit.NodeCanvas.Pathfinding
|
||||
{
|
||||
[Category("Movement/Pathfinding")]
|
||||
[Description("Makes the agent wander randomly within the navigation map")]
|
||||
public class AIWander : ActionTask<NavMeshAgent>
|
||||
{
|
||||
[Tooltip("The speed to wander with.")]
|
||||
public BBParameter<float> speed = 4;
|
||||
[Tooltip("The distance to keep from each wander point.")]
|
||||
public BBParameter<float> keepDistance = 0.1f;
|
||||
[Tooltip("A wander point can't be closer than this distance")]
|
||||
public BBParameter<float> minWanderDistance = 5;
|
||||
[Tooltip("A wander point can't be further than this distance")]
|
||||
public BBParameter<float> maxWanderDistance = 20;
|
||||
[Tooltip("If enabled, will keep wandering forever. If not, only one wander point will be performed.")]
|
||||
public bool repeat = true;
|
||||
[Tooltip("If enabled,will execute a move and exit")]
|
||||
public bool forget;
|
||||
|
||||
private Vector3? lastPosition;
|
||||
private readonly IntervalUpdate newPositionInterval=new (1);
|
||||
|
||||
private int newTargetCount;
|
||||
|
||||
protected override void OnExecute() {
|
||||
agent.speed = speed.value;
|
||||
|
||||
if (IsArrived is false)
|
||||
{
|
||||
if (lastPosition.HasValue &&
|
||||
Vector3.Distance(agent.pathEndPosition, lastPosition.Value) <= keepDistance.value)
|
||||
{
|
||||
newPositionInterval.Reset();
|
||||
EndAction();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
DoWander();
|
||||
}
|
||||
|
||||
protected override string info
|
||||
{
|
||||
get
|
||||
{
|
||||
reportBuilder.Clear();
|
||||
|
||||
reportBuilder.AppendLine(nameof(AIWander));
|
||||
reportBuilder.AppendLine($"NewTargetCount:{newTargetCount}");
|
||||
reportBuilder.AppendLine($"LastPosition:{lastPosition}");
|
||||
reportBuilder.AppendLine($"RemainDistance:{agent.remainingDistance}");
|
||||
return reportBuilder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly StringBuilder reportBuilder = new();
|
||||
|
||||
protected override void OnUpdate() {
|
||||
if ( IsArrived ) {
|
||||
if ( repeat ) {
|
||||
DoWander();
|
||||
} else {
|
||||
EndAction();
|
||||
}
|
||||
|
||||
lastPosition = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (forget)
|
||||
{
|
||||
EndAction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsArrived => !agent.pathPending && agent.remainingDistance <= agent.stoppingDistance + keepDistance.value;
|
||||
|
||||
void DoWander() {
|
||||
var min = minWanderDistance.value;
|
||||
var max = maxWanderDistance.value;
|
||||
min = Mathf.Clamp(min, 0.01f, max);
|
||||
max = Mathf.Clamp(max, min, max);
|
||||
var wanderPos = agent.transform.position;
|
||||
|
||||
if (lastPosition.HasValue && newPositionInterval.AllowUpdateWithoutReset is false)
|
||||
{
|
||||
wanderPos = lastPosition.Value;
|
||||
|
||||
agent.SetDestination(wanderPos);
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((wanderPos - agent.transform.position).magnitude < min)
|
||||
{
|
||||
wanderPos = (Random.insideUnitSphere * max) + agent.transform.position;
|
||||
}
|
||||
|
||||
NavMeshHit hit;
|
||||
if ( NavMesh.SamplePosition(wanderPos, out hit, agent.height * 2, NavMesh.AllAreas) ) {
|
||||
agent.SetDestination(hit.position);
|
||||
lastPosition = hit.position;
|
||||
newTargetCount++;
|
||||
newPositionInterval.Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPause() { OnStop(); }
|
||||
|
||||
protected override void OnStop()
|
||||
{
|
||||
if (agent.gameObject.activeSelf && agent.isOnNavMesh)
|
||||
{
|
||||
if (forget is false)
|
||||
agent.ResetPath();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe62f37bb6ab5044eba76bdd40363e0b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user