Behavior Designer の Action タスク。ターゲットを追い続け、指定距離内に捉えたら TaskStatus.Sucess を返す
using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using UnityEngine.AI;
[TaskCategory ( "MyActions" )]
public class GoToTarget : Action
{
[SerializeField] Transform target;
[SerializeField] float captureDistance;
[SerializeField] float speed;
private NavMeshAgent nav;
public override void OnStart ()
{
nav = GetComponent<NavMeshAgent> ();
nav.speed = this.speed;
nav.isStopped = false;
}
public override TaskStatus OnUpdate ()
{
nav.SetDestination ( target.position );
var distance = CalcDistanceOnGround(target.position, this.transform.position);
if ( distance < captureDistance )
{
nav.isStopped = true;
return TaskStatus.Success;
}
return TaskStatus.Running;
}
/// <summary>
/// XZ平面上での距離を返す
/// </summary>
/// <returns></returns>
private float CalcDistanceOnGround ( Vector3 a, Vector3 b )
{
var aOnGround = new Vector2(a.x, a.z);
var bOnGround = new Vector2(b.x, b.z);
return Vector2.Distance ( aOnGround, bOnGround );
}
}