From 8722a9920c1f6119bf6e769cba270e63097f8e25 Mon Sep 17 00:00:00 2001
From: chai <215380520@qq.com>
Date: Thu, 23 May 2024 10:08:29 +0800
Subject: + astar project
---
.../ExampleScripts/FollowerJumpLink.cs | 65 ++++++++++++++++++++++
1 file changed, 65 insertions(+)
create mode 100644 Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/FollowerJumpLink.cs
(limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/FollowerJumpLink.cs')
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/FollowerJumpLink.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/FollowerJumpLink.cs
new file mode 100644
index 0000000..4bd5cec
--- /dev/null
+++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/ExampleScenes/ExampleScripts/FollowerJumpLink.cs
@@ -0,0 +1,65 @@
+#if MODULE_ENTITIES
+/// [followerEntity.onTraverseOffMeshLink]
+using UnityEngine;
+using Pathfinding;
+using System.Collections;
+using Pathfinding.ECS;
+
+namespace Pathfinding.Examples {
+ [HelpURL("https://arongranberg.com/astar/documentation/stable/followerjumplink.html")]
+ public class FollowerJumpLink : MonoBehaviour, IOffMeshLinkHandler, IOffMeshLinkStateMachine {
+ // Register this class as the handler for off-mesh links when the component is enabled
+ void OnEnable() => GetComponent().onTraverseOffMeshLink = this;
+ void OnDisable() => GetComponent().onTraverseOffMeshLink = null;
+
+ IOffMeshLinkStateMachine IOffMeshLinkHandler.GetOffMeshLinkStateMachine(AgentOffMeshLinkTraversalContext context) => this;
+
+ void IOffMeshLinkStateMachine.OnFinishTraversingOffMeshLink (AgentOffMeshLinkTraversalContext context) {
+ Debug.Log("An agent finished traversing an off-mesh link");
+ }
+
+ void IOffMeshLinkStateMachine.OnAbortTraversingOffMeshLink () {
+ Debug.Log("An agent aborted traversing an off-mesh link");
+ }
+
+ IEnumerable IOffMeshLinkStateMachine.OnTraverseOffMeshLink (AgentOffMeshLinkTraversalContext ctx) {
+ var start = (Vector3)ctx.link.relativeStart;
+ var end = (Vector3)ctx.link.relativeEnd;
+ var dir = end - start;
+
+ // Disable local avoidance while traversing the off-mesh link.
+ // If it was enabled, it will be automatically re-enabled when the agent finishes traversing the link.
+ ctx.DisableLocalAvoidance();
+
+ // Move and rotate the agent to face the other side of the link.
+ // When reaching the off-mesh link, the agent may be facing the wrong direction.
+ while (!ctx.MoveTowards(
+ position: start,
+ rotation: Quaternion.LookRotation(dir, ctx.movementPlane.up),
+ gravity: true,
+ slowdown: true).reached) {
+ yield return null;
+ }
+
+ var bezierP0 = start;
+ var bezierP1 = start + Vector3.up*5;
+ var bezierP2 = end + Vector3.up*5;
+ var bezierP3 = end;
+ var jumpDuration = 1.0f;
+
+ // Animate the AI to jump from the start to the end of the link
+ for (float t = 0; t < jumpDuration; t += ctx.deltaTime) {
+ ctx.transform.Position = AstarSplines.CubicBezier(bezierP0, bezierP1, bezierP2, bezierP3, Mathf.SmoothStep(0, 1, t / jumpDuration));
+ yield return null;
+ }
+ }
+ }
+}
+/// [followerEntity.onTraverseOffMeshLink]
+#else
+using UnityEngine;
+namespace Pathfinding.Examples {
+ [HelpURL("https://arongranberg.com/astar/documentation/stable/followerjumplink.html")]
+ public class FollowerJumpLink : MonoBehaviour {}
+}
+#endif
--
cgit v1.1-26-g67d0