ResetPath.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityNavMeshAgent
  4. {
  5. [TaskCategory("Unity/NavMeshAgent")]
  6. [TaskDescription("Clears the current path. Returns Success.")]
  7. public class ResetPath : Action
  8. {
  9. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  10. public SharedGameObject targetGameObject;
  11. // cache the navmeshagent component
  12. private NavMeshAgent navMeshAgent;
  13. private GameObject prevGameObject;
  14. public override void OnStart()
  15. {
  16. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  17. if (currentGameObject != prevGameObject) {
  18. navMeshAgent = currentGameObject.GetComponent<NavMeshAgent>();
  19. prevGameObject = currentGameObject;
  20. }
  21. }
  22. public override TaskStatus OnUpdate()
  23. {
  24. if (navMeshAgent == null) {
  25. Debug.LogWarning("NavMeshAgent is null");
  26. return TaskStatus.Failure;
  27. }
  28. navMeshAgent.ResetPath();
  29. return TaskStatus.Success;
  30. }
  31. public override void OnReset()
  32. {
  33. targetGameObject = null;
  34. }
  35. }
  36. }