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 --- .../Utilities/AnimationLinkTraverser.cs | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/AnimationLinkTraverser.cs (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/AnimationLinkTraverser.cs') diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/AnimationLinkTraverser.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/AnimationLinkTraverser.cs new file mode 100644 index 0000000..455bc3e --- /dev/null +++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/AnimationLinkTraverser.cs @@ -0,0 +1,74 @@ +using System.Collections; +using UnityEngine; + +namespace Pathfinding.Examples { + using Pathfinding; + + /// + /// Example of how to handle off-mesh link traversal. + /// This is used in the "Example4_Recast_Navmesh2" example scene. + /// + /// See: + /// See: + /// See: + /// + [HelpURL("https://arongranberg.com/astar/documentation/stable/animationlinktraverser.html")] + public class AnimationLinkTraverser : VersionedMonoBehaviour { + public Animation anim; + + RichAI ai; + + void OnEnable () { + ai = GetComponent(); + if (ai != null) ai.onTraverseOffMeshLink += TraverseOffMeshLink; + } + + void OnDisable () { + if (ai != null) ai.onTraverseOffMeshLink -= TraverseOffMeshLink; + } + + protected virtual IEnumerator TraverseOffMeshLink (RichSpecial rs) { + var link = rs.nodeLink.gameObject.GetComponent(); + + if (link == null) { + Debug.LogError("Unhandled RichSpecial"); + yield break; + } + + // Rotate character to face the correct direction + while (true) { + var origRotation = ai.rotation; + var finalRotation = ai.SimulateRotationTowards(rs.first.rotation * Vector3.forward, ai.rotationSpeed * Time.deltaTime); + // Rotate until the rotation does not change anymore + if (origRotation == finalRotation) break; + ai.FinalizeMovement(ai.position, finalRotation); + yield return null; + } + + // Reposition + transform.parent.position = transform.position; + + transform.parent.rotation = transform.rotation; + transform.localPosition = Vector3.zero; + transform.localRotation = Quaternion.identity; + + // Set up animation speeds + if (rs.reverse && link.reverseAnim) { + anim[link.clip].speed = -link.animSpeed; + anim[link.clip].normalizedTime = 1; + anim.Play(link.clip); + anim.Sample(); + } else { + anim[link.clip].speed = link.animSpeed; + anim.Rewind(link.clip); + anim.Play(link.clip); + } + + // Fix required for animations in reverse direction + transform.parent.position -= transform.position-transform.parent.position; + + // Wait for the animation to finish + yield return new WaitForSeconds(Mathf.Abs(anim[link.clip].length/link.animSpeed)); + } + } +} -- cgit v1.1-26-g67d0