SetDestination.cs 1.5 KB

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