using UnityEngine;
using System.Collections;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using UnityEngine.AI;
///
/// 跟随目标
///
public class ActionFollowTarget : Action
{
///
/// 跟随的最近距离
///
public float followDistance = 2.0f;
///
/// 跟随的目标
///
public SharedTransform target;
///
/// 角色
///
private Role role = null;
///
/// 跟随距离
///
private float sqrFollowDistance = 0;
public override void OnAwake()
{
role = GetComponent();
}
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;
}
}
}