ActionSurroundAlertYC.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using BehaviorDesigner.Runtime.Tasks;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6. using BehaviorDesigner.Runtime.Tasks.EnemyAI;
  7. namespace BehaviorDesigner.Runtime.Tasks
  8. {
  9. [TaskDescription("远程警戒,前后游走")]
  10. [TaskCategory("YLSJ")]
  11. public class ActionSurroundAlertYC : NavMeshMovementBase
  12. {
  13. [Tooltip("围绕的目标")]
  14. public SharedTransform target;
  15. [Tooltip("警戒的最小总时间")]
  16. public SharedFloat alertMinTime = 4.0f;
  17. [Tooltip("警戒的最大总时间")]
  18. public SharedFloat alertMaxTime = 8.0f;
  19. [Tooltip("在当前位置后退的最小距离(比如攻击距离是2,后退的距离是1,怪物和人物之间的实际距离是2+1=3,实际距离不要超过alertDistance距离)")]
  20. public SharedFloat backMinDis = 2;
  21. [Tooltip("在当前位置后退的最大距离(比如攻击距离是2,后退的距离是3,怪物和人物之间的实际距离是2+3=5,实际距离不要超过alertDistance距离)")]
  22. public SharedFloat backMaxDis = 4;
  23. [Tooltip("警戒的最大范围,超过这个范围则进行追逐)")]
  24. public SharedFloat alertDistance = 10;
  25. [Tooltip("停留的最小时间(后退完成/围绕完成 都会停留一小段时间)")]
  26. public SharedFloat stayMinTime = 1.0f;
  27. [Tooltip("停留的最大时间(后退完成/围绕完成 都会停留一小段时间)")]
  28. public SharedFloat stayMaxTime = 2.0f;
  29. [Tooltip("旋转速度(看向目标的旋转速度)")]
  30. public float rotationSpeed = 10;
  31. /// <summary>
  32. /// 出生地的警戒范围 (从出生地算,超出这个范围则不再追击目标)
  33. /// </summary>
  34. private float liveAlertDistance = 0;
  35. /// <summary>
  36. /// 攻击距离
  37. /// </summary>
  38. private float attackDistance = 0;
  39. /// <summary>
  40. /// 是否后退中
  41. /// </summary>
  42. private bool backing = true;
  43. private float curAroundTotalTime = 0;
  44. private float curStayTime = 0;
  45. private float curBackDis = 0;
  46. private BehaviorTree behaviorTree = null;
  47. private Role role = null;
  48. private bool bSeek = false;
  49. public override void OnAwake()
  50. {
  51. base.OnAwake();
  52. behaviorTree = gameObject.GetComponent<BehaviorTree>();
  53. role = gameObject.GetComponent<Role>();
  54. }
  55. public override void OnStart()
  56. {
  57. base.OnStart();
  58. liveAlertDistance = (float)behaviorTree.GetVariable("liveAlertDistance").GetValue();
  59. attackDistance = (float)behaviorTree.GetVariable("attackDistance").GetValue();
  60. curAroundTotalTime = Random.Range(alertMinTime.Value, alertMaxTime.Value);
  61. backing = true;
  62. bSeek = false;
  63. m_NavMeshAgent.component.updateRotation = false;//禁用旋转
  64. m_NavMeshAgent.component.isStopped = false;
  65. Vector3 dir = target.Value.position - m_NavMeshAgent.component.transform.position;
  66. dir.Normalize();
  67. curBackDis = Random.Range(backMinDis.Value, backMaxDis.Value);
  68. Vector3 backPos = m_NavMeshAgent.component.transform.position - dir * curBackDis;
  69. SetDestination(backPos);
  70. }
  71. public override TaskStatus OnUpdate()
  72. {
  73. // 目标死亡
  74. if (target.Value == null)
  75. {
  76. return TaskStatus.Success;
  77. }
  78. // 时间到达
  79. curAroundTotalTime -= time.deltaTime;
  80. if (curAroundTotalTime <= 0)
  81. {
  82. return TaskStatus.Success;
  83. }
  84. float liveDis = Vector3.Distance(transform.position, role.mLivePos);
  85. float targetDis = Vector3.Distance(transform.position, target.Value.position);
  86. // 超出出生地活动范围
  87. if (liveDis > liveAlertDistance)
  88. {
  89. return TaskStatus.Success;
  90. }
  91. Vector3 centerPos = target.Value.position;
  92. Vector3 dir = transform.position - centerPos;
  93. float dis = Vector3.Distance(transform.position, centerPos);
  94. dir.Normalize();
  95. dir.y = 0;
  96. // 旋转
  97. transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(-dir), rotationSpeed * Time.deltaTime);
  98. // 超出 警戒范围
  99. if (targetDis > alertDistance.Value)
  100. {
  101. backing = false;
  102. bSeek = true;
  103. m_NavMeshAgent.component.updateRotation = true;
  104. m_NavMeshAgent.component.speed = (float)behaviorTree.GetVariable("runSpeed").GetValue();
  105. }
  106. // 停留期间不做任何处理
  107. if (curStayTime > 0)
  108. {
  109. if(role.isDie)
  110. {
  111. return TaskStatus.Success;
  112. }
  113. if (m_NavMeshAgent.component.isOnNavMesh && m_NavMeshAgent.component.isStopped != true)
  114. {
  115. m_NavMeshAgent.component.isStopped = true;
  116. }
  117. curStayTime -= time.deltaTime;
  118. return TaskStatus.Running;
  119. }
  120. else
  121. {
  122. if (role.isDie)
  123. {
  124. return TaskStatus.Success;
  125. }
  126. if (m_NavMeshAgent.component.isOnNavMesh && m_NavMeshAgent.component.isStopped != false)
  127. {
  128. m_NavMeshAgent.component.isStopped = false;
  129. }
  130. }
  131. // 后退
  132. if (backing)
  133. {
  134. if (HasArrived())
  135. {
  136. ResetPostion();
  137. }
  138. }
  139. // 围绕目标
  140. else
  141. {
  142. if (bSeek == false)
  143. {
  144. if (HasArrived())
  145. {
  146. ResetPostion();
  147. }
  148. }
  149. else
  150. {
  151. if (targetDis > curBackDis + attackDistance)
  152. {
  153. Vector3 targetDir = target.Value.position - m_NavMeshAgent.component.transform.position;
  154. targetDir.Normalize();
  155. SetDestination(target.Value.position + targetDir * (curBackDis + attackDistance));
  156. }
  157. else
  158. {
  159. bSeek = false;
  160. m_NavMeshAgent.component.updateRotation = false;
  161. m_NavMeshAgent.component.speed = (float)behaviorTree.GetVariable("moveSpeed").GetValue();
  162. ResetPostion();
  163. }
  164. }
  165. }
  166. return TaskStatus.Running;
  167. }
  168. /// <summary>
  169. /// 重置位置
  170. /// </summary>
  171. private void ResetPostion()
  172. {
  173. Vector3 dir = target.Value.position - m_NavMeshAgent.component.transform.position;
  174. dir.Normalize();
  175. if (backing)
  176. {
  177. backing = false;
  178. Vector3 frontPos = m_NavMeshAgent.component.transform.position + dir * curBackDis;
  179. SetDestination(frontPos);
  180. }
  181. else
  182. {
  183. backing = true;
  184. curBackDis = Random.Range(backMinDis.Value, backMaxDis.Value);
  185. Vector3 backPos = m_NavMeshAgent.component.transform.position - dir * curBackDis;
  186. SetDestination(backPos);
  187. }
  188. curStayTime = Random.Range(stayMinTime.Value, stayMaxTime.Value);
  189. }
  190. public override void OnEnd()
  191. {
  192. base.OnEnd();
  193. if (m_NavMeshAgent.component.isOnNavMesh)
  194. {
  195. m_NavMeshAgent.component.isStopped = true;
  196. m_NavMeshAgent.component.updateRotation = true;
  197. }
  198. }
  199. }
  200. }