A game about forced loneliness, made by TACStudios
1using UnityEngine.AI;
2
3namespace Unity.VisualScripting
4{
5#if MODULE_AI_EXISTS
6 /// <summary>
7 /// Called when the nav mesh agent comes within a certain threshold of its destination.
8 /// </summary>
9 [UnitCategory("Events/Navigation")]
10 public sealed class OnDestinationReached : MachineEventUnit<EmptyEventArgs>
11 {
12 protected override string hookName => EventHooks.Update;
13
14 /// <summary>
15 /// The threshold for the remaining distance.
16 /// </summary>
17 [DoNotSerialize]
18 public ValueInput threshold { get; private set; }
19
20 /// <summary>
21 /// Whether the event should only trigger when the path is not partial or invalid.
22 /// </summary>
23 [DoNotSerialize]
24 public ValueInput requireSuccess { get; private set; }
25
26 protected override void Definition()
27 {
28 base.Definition();
29
30 threshold = ValueInput(nameof(threshold), 0.05f);
31 requireSuccess = ValueInput(nameof(requireSuccess), true);
32 }
33
34 protected override bool ShouldTrigger(Flow flow, EmptyEventArgs args)
35 {
36 var navMeshAgent = flow.stack.gameObject.GetComponent<NavMeshAgent>();
37
38 return navMeshAgent != null &&
39 navMeshAgent.remainingDistance <= flow.GetValue<float>(threshold) &&
40 (navMeshAgent.pathStatus == NavMeshPathStatus.PathComplete || !flow.GetValue<bool>(requireSuccess));
41 }
42 }
43#endif
44}