1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System.Collections;
- using UnityEngine;
- namespace Pathfinding.Examples {
- using Pathfinding;
-
-
-
-
-
-
-
-
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_animation_link_traverser.php")]
- 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 as AnimationLink;
- if (link == null) {
- Debug.LogError("Unhandled RichSpecial");
- yield break;
- }
-
- while (true) {
- var origRotation = ai.rotation;
- var finalRotation = ai.SimulateRotationTowards(rs.first.forward, ai.rotationSpeed * Time.deltaTime);
-
- if (origRotation == finalRotation) break;
- ai.FinalizeMovement(ai.position, finalRotation);
- yield return null;
- }
-
- transform.parent.position = transform.position;
- transform.parent.rotation = transform.rotation;
- transform.localPosition = Vector3.zero;
- transform.localRotation = Quaternion.identity;
-
- 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);
- }
-
- transform.parent.position -= transform.position-transform.parent.position;
-
- yield return new WaitForSeconds(Mathf.Abs(anim[link.clip].length/link.animSpeed));
- }
- }
- }
|