123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using UnityEngine;
- namespace Pathfinding.Examples {
- using Pathfinding.Util;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_mecanim_bridge.php")]
- public class MecanimBridge : VersionedMonoBehaviour {
- public float velocitySmoothing = 1;
-
- IAstarAI ai;
-
- Animator anim;
-
- Transform tr;
- Vector3 smoothedVelocity;
-
- Vector3[] prevFootPos = new Vector3[2];
-
- Transform[] footTransforms;
- protected override void Awake () {
- base.Awake();
- ai = GetComponent<IAstarAI>();
- anim = GetComponent<Animator>();
- tr = transform;
-
- footTransforms = new [] { anim.GetBoneTransform(HumanBodyBones.LeftFoot), anim.GetBoneTransform(HumanBodyBones.RightFoot) };
- }
-
- void Update () {
- var aiBase = ai as AIBase;
- aiBase.canMove = false;
-
-
- }
-
- Vector3 CalculateBlendPoint () {
-
- if (footTransforms[0] == null || footTransforms[1] == null) return tr.position;
- var leftFootPos = footTransforms[0].position;
- var rightFootPos = footTransforms[1].position;
-
-
-
- var footVelocity1 = (leftFootPos - prevFootPos[0]) / Time.deltaTime;
- var footVelocity2 = (rightFootPos - prevFootPos[1]) / Time.deltaTime;
- float denominator = footVelocity1.magnitude + footVelocity2.magnitude;
- var pivotWeight = denominator > 0 ? footVelocity1.magnitude / denominator : 0.5f;
- prevFootPos[0] = leftFootPos;
- prevFootPos[1] = rightFootPos;
- var pivotPosition = Vector3.Lerp(leftFootPos, rightFootPos, pivotWeight);
- return pivotPosition;
- }
- void OnAnimatorMove () {
- Vector3 nextPosition;
- Quaternion nextRotation;
- ai.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
-
- var desiredVelocity = ai.desiredVelocity;
- var desiredVelocityWithoutGrav = desiredVelocity;
- desiredVelocityWithoutGrav.y = 0;
- anim.SetFloat("InputMagnitude", ai.reachedEndOfPath || desiredVelocityWithoutGrav.magnitude < 0.1f ? 0f : 1f);
-
- var localDesiredVelocity = tr.InverseTransformDirection(desiredVelocityWithoutGrav);
- smoothedVelocity = Vector3.Lerp(smoothedVelocity, localDesiredVelocity, velocitySmoothing > 0 ? Time.deltaTime / velocitySmoothing : 1);
- if (smoothedVelocity.magnitude < 0.4f) {
- smoothedVelocity = smoothedVelocity.normalized * 0.4f;
- }
- anim.SetFloat("X", smoothedVelocity.x);
- anim.SetFloat("Y", smoothedVelocity.z);
-
-
- var rotationSpeed = 360f;
- if (ai is AIPath aipath) {
- rotationSpeed = aipath.rotationSpeed;
- } else if (ai is RichAI richai) {
- rotationSpeed = richai.rotationSpeed;
- }
-
- var newRot = RotateTowards(desiredVelocityWithoutGrav, Time.deltaTime * rotationSpeed);
-
- nextPosition = ai.position;
- nextRotation = ai.rotation;
- nextPosition = RotatePointAround(nextPosition, CalculateBlendPoint(), newRot * Quaternion.Inverse(nextRotation));
- nextRotation = newRot;
-
- nextRotation = anim.deltaRotation * nextRotation;
-
- var deltaPos = anim.deltaPosition;
- deltaPos.y = desiredVelocity.y * Time.deltaTime;
- nextPosition += deltaPos;
-
- ai.FinalizeMovement(nextPosition, nextRotation);
- }
- static Vector3 RotatePointAround (Vector3 point, Vector3 around, Quaternion rotation) {
- return rotation * (point - around) + around;
- }
-
-
-
-
-
-
- protected virtual Quaternion RotateTowards (Vector3 direction, float maxDegrees) {
- if (direction != Vector3.zero) {
- Quaternion targetRotation = Quaternion.LookRotation(direction);
- return Quaternion.RotateTowards(tr.rotation, targetRotation, maxDegrees);
- } else {
- return tr.rotation;
- }
- }
- }
- }
|