CombatHeroFollowMoveState.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Animancer;
  2. using GameLogic.Combat.CombatTool;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. namespace GameLogic.Combat.Hero.State
  6. {
  7. public class CombatHeroFollowMoveState : CombatHeroStateBasic
  8. {
  9. protected Vector3 lasetTaregtPoint;
  10. protected Vector3 lastMovePoint;
  11. protected AnimancerState _transitionAsset;
  12. public float defMoveSpeed;
  13. public CombatHeroFollowMoveState(CombatHeroEntity combatHeroEntity) : base(combatHeroEntity)
  14. {
  15. }
  16. protected override void ProEnter()
  17. {
  18. lasetTaregtPoint = new Vector3(999999, 99999, 99999);
  19. _transitionAsset = combatHeroEntity.combatHeroAnimtion.Play("run");
  20. combatHeroEntity.CombatAIBasic.NavMeshAgent.updateRotation = false;
  21. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = true;
  22. combatHeroEntity.CombatAIBasic.NavMeshAgent.obstacleAvoidanceType =
  23. ObstacleAvoidanceType.HighQualityObstacleAvoidance;
  24. combatHeroEntity.CombatAIBasic.NavMeshAgent.avoidancePriority = 20;
  25. defMoveSpeed = combatHeroEntity.CombatAIBasic.NavMeshAgent.speed;
  26. }
  27. protected override void ProExit()
  28. {
  29. combatHeroEntity.CombatAIBasic.NavMeshAgent.speed = defMoveSpeed;
  30. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = true;
  31. combatHeroEntity.CombatAIBasic.NavMeshAgent.velocity = Vector3.zero;
  32. combatHeroEntity.CombatAIBasic.NavMeshAgent.avoidancePriority = 50;
  33. // combatHeroEntity.CombatAIBasic.NavMeshAgent.acceleration = 0;
  34. }
  35. protected void SetNewPath(Vector3 taregtPos)
  36. {
  37. lastMovePoint = combatHeroEntity.dotPos;
  38. lasetTaregtPoint = taregtPos;
  39. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = false;
  40. // combatHeroEntity.CombatAIBasic.NavMeshAgent.acceleration = 8;
  41. combatHeroEntity.CombatAIBasic.NavMeshAgent.SetDestination(taregtPos);
  42. }
  43. protected override void ProUpdate(float t)
  44. {
  45. // if (combatHeroEntity.CombatAIBasic.currFocusTarget == null)
  46. // {
  47. // combatHeroEntity.CombatAIBasic.ChangeState(CombatHeroStateType.idle);
  48. // return;
  49. // }
  50. Vector3 targetPos = CombatController.currActiveCombat.CombatHeroController.GetFollowPos(combatHeroEntity);
  51. Vector3 myPos = combatHeroEntity.dotPos;
  52. float bl = Vector3.SqrMagnitude(lasetTaregtPoint - targetPos) / 1f;
  53. combatHeroEntity.CombatAIBasic.NavMeshAgent.speed = bl * 2.5f + defMoveSpeed;
  54. if (Vector3.SqrMagnitude(lasetTaregtPoint - targetPos) > 1)
  55. {
  56. SetNewPath(targetPos);
  57. }
  58. // else
  59. // {
  60. // combatHeroEntity.CombatAIBasic.ChangeState(CombatHeroStateType.followIdle);
  61. // return;
  62. // }
  63. if (_transitionAsset != null)
  64. {
  65. float v = combatHeroEntity.CombatAIBasic.NavMeshAgent.velocity.sqrMagnitude;
  66. float v2 = v / 36;
  67. _transitionAsset.Speed = Mathf.Clamp(v2, 0.2f, 1.5f);
  68. }
  69. if (!combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped)
  70. {
  71. Vector3 nextPos = myPos;
  72. Vector3 p = nextPos - lastMovePoint;
  73. lastMovePoint = myPos;
  74. if (p.sqrMagnitude > 0.00001)
  75. {
  76. combatHeroEntity.combatHeroGameObject.rotation = Quaternion.Lerp(
  77. combatHeroEntity.combatHeroGameObject.rotation, Quaternion.LookRotation(p.normalized), 0.5f);
  78. }
  79. }
  80. }
  81. }
  82. }