using UnityEngine.AI;
namespace Unity.VisualScripting
{
#if MODULE_AI_EXISTS
///
/// Called when the nav mesh agent comes within a certain threshold of its destination.
///
[UnitCategory("Events/Navigation")]
public sealed class OnDestinationReached : MachineEventUnit
{
protected override string hookName => EventHooks.Update;
///
/// The threshold for the remaining distance.
///
[DoNotSerialize]
public ValueInput threshold { get; private set; }
///
/// Whether the event should only trigger when the path is not partial or invalid.
///
[DoNotSerialize]
public ValueInput requireSuccess { get; private set; }
protected override void Definition()
{
base.Definition();
threshold = ValueInput(nameof(threshold), 0.05f);
requireSuccess = ValueInput(nameof(requireSuccess), true);
}
protected override bool ShouldTrigger(Flow flow, EmptyEventArgs args)
{
var navMeshAgent = flow.stack.gameObject.GetComponent();
return navMeshAgent != null &&
navMeshAgent.remainingDistance <= flow.GetValue(threshold) &&
(navMeshAgent.pathStatus == NavMeshPathStatus.PathComplete || !flow.GetValue(requireSuccess));
}
}
#endif
}