ActionFlee.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Movement
  3. {
  4. [TaskDescription("Flee from the target specified using the Unity NavMesh.")]
  5. [TaskCategory("Movement")]
  6. [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/Movement/documentation.php?id=4")]
  7. [TaskIcon("Assets/Behavior Designer Movement/Editor/Icons/{SkinColor}FleeIcon.png")]
  8. public class ActionFlee : Action
  9. {
  10. [Tooltip("The speed of the agent")]
  11. public SharedFloat speed;
  12. [Tooltip("Angular speed of the agent")]
  13. public SharedFloat angularSpeed;
  14. [Tooltip("The agent has fleed when the square magnitude is greater than this value")]
  15. public SharedFloat fleedMinDistance = 5;
  16. [Tooltip("The agent has fleed when the square magnitude is greater than this value")]
  17. public SharedFloat fleedMaxDistance = 15;
  18. [Tooltip("The distance to look ahead when fleeing")]
  19. public SharedFloat lookAheadDistance = 5;
  20. [Tooltip("The transform that the agent is fleeing from")]
  21. public SharedTransform targetTransform;
  22. [Tooltip("If target is null then use the target position")]
  23. public SharedVector3 targetPosition;
  24. // True if the target is a transform
  25. private bool dynamicTarget;
  26. // A cache of the NavMeshAgent
  27. private UnityEngine.AI.NavMeshAgent navMeshAgent;
  28. private float fleedDistance;
  29. public override void OnAwake()
  30. {
  31. // cache for quick lookup
  32. navMeshAgent = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
  33. }
  34. public override void OnStart()
  35. {
  36. if (navMeshAgent == null)
  37. {
  38. navMeshAgent = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();
  39. }
  40. // the target is dynamic if the target transform is not null and has a valid
  41. dynamicTarget = (targetTransform != null && targetTransform.Value != null);
  42. fleedDistance = UnityEngine.Random.Range(fleedMinDistance.Value, fleedMaxDistance.Value);
  43. // set the speed, angular speed, and destination then enable the agent
  44. navMeshAgent.speed = speed.Value;
  45. navMeshAgent.angularSpeed = angularSpeed.Value;
  46. navMeshAgent.enabled = true;
  47. navMeshAgent.destination = Target();
  48. }
  49. // Flee from the target. Return success once the agent has fleed the target by moving far enough away from it
  50. // Return running if the agent is still fleeing
  51. public override TaskStatus OnUpdate()
  52. {
  53. if (!navMeshAgent.pathPending && Vector3.SqrMagnitude(transform.position - Target()) > fleedDistance) {
  54. return TaskStatus.Success;
  55. }
  56. navMeshAgent.destination = FleePosition();
  57. return TaskStatus.Running;
  58. }
  59. public override void OnEnd()
  60. {
  61. // Disable the nav mesh
  62. navMeshAgent.enabled = false;
  63. }
  64. // return targetPosition if targetTransform is null
  65. private Vector3 Target()
  66. {
  67. if (dynamicTarget) {
  68. return targetTransform.Value.position;
  69. }
  70. return targetPosition.Value;
  71. }
  72. // Flee in the opposite direction
  73. private Vector3 FleePosition()
  74. {
  75. return transform.position + (transform.position - Target()).normalized * lookAheadDistance.Value;
  76. }
  77. // Reset the public variables
  78. public override void OnReset()
  79. {
  80. speed = 10;
  81. angularSpeed = 10;
  82. lookAheadDistance = 5;
  83. }
  84. }
  85. }