CombatHeroFollowMoveState.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 HeroPlayStateInfoBasic _transitionAsset;
  12. public float defMoveSpeed;
  13. public CombatHeroFollowMoveState(CombatHeroEntity combatHeroEntity) : base(combatHeroEntity)
  14. {
  15. }
  16. public override bool IsUpdateLockTarget()
  17. {
  18. return false;
  19. }
  20. protected override void ProEnter()
  21. {
  22. lasetTaregtPoint = new Vector3(999999, 99999, 99999);
  23. _transitionAsset = combatHeroEntity.combatHeroAnimtion.Play("run");
  24. combatHeroEntity.CombatAIBasic.NavMeshAgent.updateRotation = false;
  25. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = true;
  26. combatHeroEntity.CombatAIBasic.NavMeshAgent.obstacleAvoidanceType =
  27. ObstacleAvoidanceType.HighQualityObstacleAvoidance;
  28. combatHeroEntity.CombatAIBasic.NavMeshAgent.avoidancePriority = 20;
  29. defMoveSpeed = combatHeroEntity.CombatAIBasic.NavMeshAgent.speed;
  30. }
  31. protected override void ProExit()
  32. {
  33. combatHeroEntity.CombatAIBasic.NavMeshAgent.speed = defMoveSpeed;
  34. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = true;
  35. combatHeroEntity.CombatAIBasic.NavMeshAgent.velocity = Vector3.zero;
  36. combatHeroEntity.CombatAIBasic.NavMeshAgent.avoidancePriority = 50;
  37. // combatHeroEntity.CombatAIBasic.NavMeshAgent.acceleration = 0;
  38. }
  39. protected void SetNewPath(Vector3 taregtPos)
  40. {
  41. lastMovePoint = combatHeroEntity.dotPos;
  42. lasetTaregtPoint = taregtPos;
  43. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = false;
  44. // combatHeroEntity.CombatAIBasic.NavMeshAgent.acceleration = 8;
  45. combatHeroEntity.CombatAIBasic.NavMeshAgent.SetDestination(taregtPos);
  46. }
  47. protected override void ProUpdate(float t)
  48. {
  49. // if (combatHeroEntity.CombatAIBasic.currFocusTarget == null)
  50. // {
  51. // combatHeroEntity.CombatAIBasic.ChangeState(CombatHeroStateType.idle);
  52. // return;
  53. // }
  54. Vector3 targetPos = CombatController.currActiveCombat.CombatHeroController.GetFollowPos(combatHeroEntity);
  55. Vector3 myPos = combatHeroEntity.dotPos;
  56. float bl = Vector3.SqrMagnitude(lasetTaregtPoint - targetPos) / 1f;
  57. combatHeroEntity.CombatAIBasic.NavMeshAgent.speed = bl * 2.5f + defMoveSpeed;
  58. if (Vector3.SqrMagnitude(lasetTaregtPoint - targetPos) > 1)
  59. {
  60. SetNewPath(targetPos);
  61. }
  62. else
  63. {
  64. if (Vector3.SqrMagnitude(myPos - targetPos) < 0.1f)
  65. {
  66. combatHeroEntity.CombatAIBasic.ChangeState(CombatHeroStateType.followIdle);
  67. return;
  68. }
  69. }
  70. if (_transitionAsset != null)
  71. {
  72. float v = combatHeroEntity.CombatAIBasic.NavMeshAgent.velocity.sqrMagnitude;
  73. float v2 = v / 36;
  74. _transitionAsset.SetSpeed(Mathf.Clamp(v2, 0.2f, 1.5f));
  75. }
  76. if (!combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped)
  77. {
  78. Vector3 nextPos = myPos;
  79. Vector3 p = nextPos - lastMovePoint;
  80. lastMovePoint = myPos;
  81. if (p.sqrMagnitude > 0.0001)
  82. {
  83. combatHeroEntity.combatHeroGameObject.rotation = Quaternion.Lerp(
  84. combatHeroEntity.combatHeroGameObject.rotation, Quaternion.LookRotation(p.normalized), 0.5f);
  85. }
  86. }
  87. }
  88. }
  89. }