CombatHeroFollowMoveState.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. 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. if (Vector3.SqrMagnitude(myPos - targetPos) < 0.1f)
  61. {
  62. combatHeroEntity.CombatAIBasic.ChangeState(CombatHeroStateType.followIdle);
  63. return;
  64. }
  65. }
  66. if (_transitionAsset != null)
  67. {
  68. float v = combatHeroEntity.CombatAIBasic.NavMeshAgent.velocity.sqrMagnitude;
  69. float v2 = v / 36;
  70. _transitionAsset.SetSpeed(Mathf.Clamp(v2, 0.2f, 1.5f));
  71. }
  72. if (!combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped)
  73. {
  74. Vector3 nextPos = myPos;
  75. Vector3 p = nextPos - lastMovePoint;
  76. lastMovePoint = myPos;
  77. if (p.sqrMagnitude > 0.0001)
  78. {
  79. combatHeroEntity.combatHeroGameObject.rotation = Quaternion.Lerp(
  80. combatHeroEntity.combatHeroGameObject.rotation, Quaternion.LookRotation(p.normalized), 0.5f);
  81. }
  82. }
  83. }
  84. }
  85. }