diff options
author | chai <215380520@qq.com> | 2024-05-23 10:08:29 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-05-23 10:08:29 +0800 |
commit | 8722a9920c1f6119bf6e769cba270e63097f8e25 (patch) | |
tree | 2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/AnimationLinkTraverser.cs | |
parent | 3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff) |
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/AnimationLinkTraverser.cs')
-rw-r--r-- | Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/AnimationLinkTraverser.cs | 74 |
1 files changed, 74 insertions, 0 deletions
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; + + /// <summary> + /// Example of how to handle off-mesh link traversal. + /// This is used in the "Example4_Recast_Navmesh2" example scene. + /// + /// See: <see cref="Pathfinding.RichAI"/> + /// See: <see cref="Pathfinding.RichAI.onTraverseOffMeshLink"/> + /// See: <see cref="Pathfinding.AnimationLink"/> + /// </summary> + [HelpURL("https://arongranberg.com/astar/documentation/stable/animationlinktraverser.html")] + public class AnimationLinkTraverser : VersionedMonoBehaviour { + public Animation anim; + + RichAI ai; + + void OnEnable () { + ai = GetComponent<RichAI>(); + 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<AnimationLink>(); + + 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)); + } + } +} |