summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Behaviors/Patrol.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-05-23 10:08:29 +0800
committerchai <215380520@qq.com>2024-05-23 10:08:29 +0800
commit8722a9920c1f6119bf6e769cba270e63097f8e25 (patch)
tree2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Behaviors/Patrol.cs
parent3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff)
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Behaviors/Patrol.cs')
-rw-r--r--Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Behaviors/Patrol.cs68
1 files changed, 68 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Behaviors/Patrol.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Behaviors/Patrol.cs
new file mode 100644
index 0000000..47f4c00
--- /dev/null
+++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Behaviors/Patrol.cs
@@ -0,0 +1,68 @@
+using UnityEngine;
+using System.Collections;
+
+namespace Pathfinding {
+ /// <summary>
+ /// Simple patrol behavior.
+ /// This will set the destination on the agent so that it moves through the sequence of objects in the <see cref="targets"/> array.
+ /// Upon reaching a target it will wait for <see cref="delay"/> seconds.
+ ///
+ /// [Open online documentation to see videos]
+ ///
+ /// See: <see cref="Pathfinding.AIDestinationSetter"/>
+ /// See: <see cref="Pathfinding.AIPath"/>
+ /// See: <see cref="Pathfinding.RichAI"/>
+ /// See: <see cref="Pathfinding.AILerp"/>
+ /// </summary>
+ [UniqueComponent(tag = "ai.destination")]
+ [AddComponentMenu("Pathfinding/AI/Behaviors/Patrol")]
+ [HelpURL("https://arongranberg.com/astar/documentation/stable/patrol.html")]
+ public class Patrol : VersionedMonoBehaviour {
+ /// <summary>Target points to move to in order</summary>
+ public Transform[] targets;
+
+ /// <summary>Time in seconds to wait at each target</summary>
+ public float delay = 0;
+
+ /// <summary>
+ /// If true, the agent's destination will be updated every frame instead of only when switching targets.
+ ///
+ /// This is good if you have moving targets, but is otherwise unnecessary and slightly slower.
+ /// </summary>
+ public bool updateDestinationEveryFrame = false;
+
+ /// <summary>Current target index</summary>
+ int index = -1;
+
+ IAstarAI agent;
+ float switchTime = float.NegativeInfinity;
+
+ protected override void Awake () {
+ base.Awake();
+ agent = GetComponent<IAstarAI>();
+ }
+
+ /// <summary>Update is called once per frame</summary>
+ void Update () {
+ if (targets.Length == 0) return;
+
+ // Note: using reachedEndOfPath and pathPending instead of reachedDestination here because
+ // if the destination cannot be reached by the agent, we don't want it to get stuck, we just want it to get as close as possible and then move on.
+ if (agent.reachedEndOfPath && !agent.pathPending && float.IsPositiveInfinity(switchTime)) {
+ switchTime = Time.time + delay;
+ }
+
+ if (Time.time >= switchTime) {
+ index++;
+ switchTime = float.PositiveInfinity;
+
+ index = index % targets.Length;
+ agent.destination = targets[index].position;
+ agent.SearchPath();
+ } else if (updateDestinationEveryFrame) {
+ index = index % targets.Length;
+ agent.destination = targets[index].position;
+ }
+ }
+ }
+}