12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using UnityEngine;
- using System.Collections;
- using BehaviorDesigner.Runtime;
- using BehaviorDesigner.Runtime.Tasks;
- using UnityEngine.AI;
- /// <summary>
- /// 跟随目标
- /// </summary>
- public class ActionFollowTarget : Action
- {
- /// <summary>
- /// 跟随的最近距离
- /// </summary>
- public float followDistance = 2.0f;
- /// <summary>
- /// 跟随的目标
- /// </summary>
- public SharedTransform target;
- /// <summary>
- /// 角色
- /// </summary>
- private Role role = null;
- /// <summary>
- /// 跟随距离
- /// </summary>
- private float sqrFollowDistance = 0;
- public override void OnAwake()
- {
- role = GetComponent<Role>();
- }
- public override void OnStart()
- {
- sqrFollowDistance = followDistance * followDistance;
- //启用导航组件
- role.mNavMeshAgent.enabled = true;
- if (target != null && target.Value != null)
- {
- role.mNavMeshAgent.destination = target.Value.position;
- }
- }
- public override void OnEnd()
- {
- role.mNavMeshAgent.enabled = false;
- }
- //如果抢夺者在视野内,就追, 否则就认为防御成功
- public override TaskStatus OnUpdate()
- {
- //做一个安全的校验
- if (target == null && target.Value == null)
- {
- return TaskStatus.Failure;
- }
- float sqrDistance = (target.Value.position - transform.position).sqrMagnitude;
- if (sqrDistance > sqrFollowDistance)
- {
- if (role.mNavMeshAgent.destination != target.Value.position)
- {
- role.mNavMeshAgent.destination = target.Value.position;
- }
- return TaskStatus.Running;
- }
- else
- {
- return TaskStatus.Success;
- }
- }
- }
|