ChaseBehavior.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace EnemyAI
  5. {
  6. internal class ChaseBehavior : Behavior
  7. {
  8. private const float playerDistanceThreshold = .5f;
  9. internal override Result Run(ref Blackboard blackboard)
  10. {
  11. blackboard.navMeshAgent.isStopped = false;
  12. if (!blackboard.Night)
  13. {
  14. blackboard.navMeshAgent.isStopped = true;
  15. return Result.Failure;
  16. }
  17. if (Vector3.Distance(blackboard.PlayerPosition, blackboard.EnemyPosition) < playerDistanceThreshold)
  18. {
  19. //Close enough to player
  20. return Result.Success;
  21. }
  22. //If the player moved so far that re-pathing is necessary
  23. if (Vector3.Distance(blackboard.PlayerPosition, blackboard.navMeshAgent.destination) > playerDistanceThreshold)
  24. {
  25. blackboard.navMeshAgent.destination = blackboard.PlayerPosition;
  26. }
  27. return Result.Running;
  28. }
  29. }
  30. }