123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using BehaviorDesigner.Runtime.Tasks.EnemyAI;
- using UnityEngine;
- using UnityEngine.AI;
- using UnityGameFramework.Runtime;
- namespace BehaviorDesigner.Runtime.Tasks.Movement
- {
- [TaskDescription("ToTarget using the Unity NavMesh.")]
- [TaskCategory("YLSJ")]
- [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/Movement/documentation.php?id=9")]
- [TaskIcon("Assets/Behavior Designer Movement/Editor/Icons/{SkinColor}WanderIcon.png")]
- public class ActionMoveToTarget : NavMeshMovementBase
- {
- [Tooltip("终点目标Object")]
- public SharedTransform targetEndObj;
- /// <summary>
- /// 跟随的目标
- /// </summary>
- public SharedTransform target;
- public SharedTransform targetObject;
- [Tooltip("检测目标的层级")]
- public LayerMask objectLayerMask;
- public SharedFloat viewDistance = 10;
- // 是否移动中
- private bool bMove = false;
- private Vector3 movePos = Vector3.zero;
- /// <summary>
- /// 出生最大距离
- /// </summary>
- private float liveAlertDistance = 0;
- private BehaviorTree behaviorTree = null;
- /// <summary>
- /// 路径点顺序/倒叙
- /// </summary>
- private bool bPointAdd = true;
- /// <summary>
- /// 是否赶往出生点
- /// </summary>
- private bool bToLive = false;
- private Role role = null;
- public override void OnAwake()
- {
- base.OnAwake();
- behaviorTree = gameObject.GetComponent<BehaviorTree>();
- }
- public override void OnStart()
- {
- targetEndObj.Value = GameObject.Find("AIManager/EndPoint").transform;
- base.OnStart();
- role = GetComponent<Role>();
- liveAlertDistance = (float)behaviorTree.GetVariable("liveAlertDistance").GetValue();
- if (m_NavMeshAgent.component.isOnNavMesh)
- {
- m_NavMeshAgent.component.isStopped = false;
- }
- movePos = targetEndObj.Value.position;
- SetDestination(movePos);
- }
- public override TaskStatus OnUpdate()
- {
-
- Transform objectFound = null;
- target.Value = null;
- //var hitColliders = Physics.OverlapSphere(transform.position, viewDistance.Value, objectLayerMask);
- //if (hitColliders != null)
- //{
- // for (int i = 0; i < hitColliders.Length; ++i)
- // {
- // if (hitColliders[i].gameObject.activeSelf == false)
- // {
- // continue;
- // }
- // Role role = hitColliders[i].gameObject.GetComponent<Role>();
- // if (role == null || role.isDie)
- // {
- // continue;
- // }
- // target.Value = role.transform;
- // }
- //}
- //if (target != null && target.Value != null)
- //{
- // float sqrDistance = (target.Value.position - transform.position).sqrMagnitude;
- // if (sqrDistance < viewDistance.Value)
- // {
- // //SetDestination(target.Value.position);
- // targetObject.Value = target.Value;
- // return TaskStatus.Success;
- // }
- //}
- SetDestination(movePos);
- if (Vector3.Distance(transform.position, targetEndObj.Value.position) < 1.5f)
- {
- EventComponent eventCmpt = GameEntry.GetComponent<EventComponent>();
- eventCmpt.FireNow(this, new DefenseEventEscapeNum());
- RoleManager.Instance.DeleteRole(role);
- }
- //else
- //{
- // SetDestination(targetEndObj.Value.position);
- //}
- return TaskStatus.Running;
- }
- public override void OnEnd()
- {
- base.OnEnd();
- }
- protected bool SetDestination(Vector3 destination)
- {
- NavMeshHit hit;
- bool has = NavMesh.SamplePosition(destination, out hit, 5, NavMesh.AllAreas);
- if (has && m_NavMeshAgent.component.isOnNavMesh)
- {
- m_NavMeshAgent.component.SetDestination(hit.position);
- }
- return true;
- }
- // Reset the public variables
- public override void OnReset()
- {
- }
- }
- }
|