EnemyAI.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. public class EnemyAI : MonoBehaviour
  6. {
  7. public List<Transform> patrolPoints;
  8. private UnityEngine.AI.NavMeshAgent navMeshAgent;
  9. [SerializeField] int currentTarget;
  10. [SerializeField] bool targetReached;
  11. [SerializeField] float chaseRadius;
  12. public enum EnemyState
  13. {
  14. Idle,
  15. Patrol,
  16. Chase
  17. }
  18. [SerializeField] EnemyState currentEnemyState = EnemyState.Patrol;
  19. private void Update()
  20. {
  21. switch (currentEnemyState)
  22. {
  23. case EnemyState.Idle:
  24. break;
  25. case EnemyState.Patrol:
  26. Patrol();
  27. break;
  28. case EnemyState.Chase:
  29. //Chase();
  30. break;
  31. default:
  32. Debug.LogError("No state for enemy");
  33. break;
  34. }
  35. }
  36. void Patrol()
  37. {
  38. currentEnemyState = EnemyState.Patrol;
  39. if(patrolPoints.Count > 0 && patrolPoints[currentTarget])
  40. {
  41. navMeshAgent.destination = patrolPoints[currentTarget].position;
  42. float distance = Vector3.Distance(transform.position, patrolPoints[currentTarget].position);
  43. if(distance < 0.1f && !targetReached)
  44. {
  45. targetReached = true;
  46. }
  47. else if(distance < 0.1f && targetReached)
  48. {
  49. StartCoroutine(WaitBeforeMoving());
  50. }
  51. }
  52. }
  53. /* void Chase()
  54. {
  55. }*/
  56. IEnumerator WaitBeforeMoving()
  57. {
  58. yield return new WaitForSeconds(1f);
  59. targetReached = false;
  60. }
  61. }