| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class EnemyAI : MonoBehaviour
- {
- public List<Transform> patrolPoints;
- private UnityEngine.AI.NavMeshAgent navMeshAgent;
- [SerializeField] int currentTarget;
- [SerializeField] bool targetReached;
- [SerializeField] float chaseRadius;
- public enum EnemyState
- {
- Idle,
- Patrol,
- Chase
- }
- [SerializeField] EnemyState currentEnemyState = EnemyState.Patrol;
- private void Update()
- {
- switch (currentEnemyState)
- {
- case EnemyState.Idle:
- break;
- case EnemyState.Patrol:
- Patrol();
- break;
- case EnemyState.Chase:
- //Chase();
- break;
- default:
- Debug.LogError("No state for enemy");
- break;
- }
- }
- void Patrol()
- {
- currentEnemyState = EnemyState.Patrol;
- if(patrolPoints.Count > 0 && patrolPoints[currentTarget])
- {
- navMeshAgent.destination = patrolPoints[currentTarget].position;
- float distance = Vector3.Distance(transform.position, patrolPoints[currentTarget].position);
- if(distance < 0.1f && !targetReached)
- {
- targetReached = true;
- }
- else if(distance < 0.1f && targetReached)
- {
- StartCoroutine(WaitBeforeMoving());
- }
- }
- }
- /* void Chase()
- {
-
- }*/
- IEnumerator WaitBeforeMoving()
- {
- yield return new WaitForSeconds(1f);
- targetReached = false;
- }
- }
|