ActionMoveToTarget.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using BehaviorDesigner.Runtime.Tasks.EnemyAI;
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. using UnityGameFramework.Runtime;
  5. namespace BehaviorDesigner.Runtime.Tasks.Movement
  6. {
  7. [TaskDescription("ToTarget using the Unity NavMesh.")]
  8. [TaskCategory("YLSJ")]
  9. [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/Movement/documentation.php?id=9")]
  10. [TaskIcon("Assets/Behavior Designer Movement/Editor/Icons/{SkinColor}WanderIcon.png")]
  11. public class ActionMoveToTarget : NavMeshMovementBase
  12. {
  13. [Tooltip("终点目标Object")]
  14. public SharedTransform targetEndObj;
  15. /// <summary>
  16. /// 跟随的目标
  17. /// </summary>
  18. public SharedTransform target;
  19. public SharedTransform targetObject;
  20. [Tooltip("检测目标的层级")]
  21. public LayerMask objectLayerMask;
  22. public SharedFloat viewDistance = 10;
  23. // 是否移动中
  24. private bool bMove = false;
  25. private Vector3 movePos = Vector3.zero;
  26. /// <summary>
  27. /// 出生最大距离
  28. /// </summary>
  29. private float liveAlertDistance = 0;
  30. private BehaviorTree behaviorTree = null;
  31. /// <summary>
  32. /// 路径点顺序/倒叙
  33. /// </summary>
  34. private bool bPointAdd = true;
  35. /// <summary>
  36. /// 是否赶往出生点
  37. /// </summary>
  38. private bool bToLive = false;
  39. private Role role = null;
  40. public override void OnAwake()
  41. {
  42. base.OnAwake();
  43. behaviorTree = gameObject.GetComponent<BehaviorTree>();
  44. }
  45. public override void OnStart()
  46. {
  47. targetEndObj.Value = GameObject.Find("AIManager/EndPoint").transform;
  48. base.OnStart();
  49. role = GetComponent<Role>();
  50. liveAlertDistance = (float)behaviorTree.GetVariable("liveAlertDistance").GetValue();
  51. if (m_NavMeshAgent.component.isOnNavMesh)
  52. {
  53. m_NavMeshAgent.component.isStopped = false;
  54. }
  55. movePos = targetEndObj.Value.position;
  56. SetDestination(movePos);
  57. }
  58. public override TaskStatus OnUpdate()
  59. {
  60. Transform objectFound = null;
  61. target.Value = null;
  62. //var hitColliders = Physics.OverlapSphere(transform.position, viewDistance.Value, objectLayerMask);
  63. //if (hitColliders != null)
  64. //{
  65. // for (int i = 0; i < hitColliders.Length; ++i)
  66. // {
  67. // if (hitColliders[i].gameObject.activeSelf == false)
  68. // {
  69. // continue;
  70. // }
  71. // Role role = hitColliders[i].gameObject.GetComponent<Role>();
  72. // if (role == null || role.isDie)
  73. // {
  74. // continue;
  75. // }
  76. // target.Value = role.transform;
  77. // }
  78. //}
  79. //if (target != null && target.Value != null)
  80. //{
  81. // float sqrDistance = (target.Value.position - transform.position).sqrMagnitude;
  82. // if (sqrDistance < viewDistance.Value)
  83. // {
  84. // //SetDestination(target.Value.position);
  85. // targetObject.Value = target.Value;
  86. // return TaskStatus.Success;
  87. // }
  88. //}
  89. SetDestination(movePos);
  90. if (Vector3.Distance(transform.position, targetEndObj.Value.position) < 1.5f)
  91. {
  92. EventComponent eventCmpt = GameEntry.GetComponent<EventComponent>();
  93. eventCmpt.FireNow(this, new DefenseEventEscapeNum());
  94. RoleManager.Instance.DeleteRole(role);
  95. }
  96. //else
  97. //{
  98. // SetDestination(targetEndObj.Value.position);
  99. //}
  100. return TaskStatus.Running;
  101. }
  102. public override void OnEnd()
  103. {
  104. base.OnEnd();
  105. }
  106. protected bool SetDestination(Vector3 destination)
  107. {
  108. NavMeshHit hit;
  109. bool has = NavMesh.SamplePosition(destination, out hit, 5, NavMesh.AllAreas);
  110. if (has && m_NavMeshAgent.component.isOnNavMesh)
  111. {
  112. m_NavMeshAgent.component.SetDestination(hit.position);
  113. }
  114. return true;
  115. }
  116. // Reset the public variables
  117. public override void OnReset()
  118. {
  119. }
  120. }
  121. }