ActionMoveToTarget.cs 4.2 KB

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