SetAcceleration.cs 1.5 KB

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