using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyController : MonoBehaviour { private NavMeshAgent agent; private Vector3 startingPos; private Animator anim; private AudioSource aSource; private GameObject player; private GameManager gameMgr; public List<Transform> navPoints = new List<Transform>(); public bool mainEnemy; private bool engagePlayer; private void Awake() { if(mainEnemy) { agent = GetComponent<NavMeshAgent>(); anim = GetComponent<Animator>(); } else { agent = GetComponent<NavMeshAgent>(); } aSource = GetComponent<AudioSource>(); startingPos = transform.position; } private void Start() { player = ReferenceManager.GetObject(8); gameMgr = ReferenceManager.GetGameManager(); } private void Update() { Vector3 playerPos = player.transform.position; if(!engagePlayer) { if (Vector3.Distance(transform.position, playerPos) <= 17.5f) { agent.SetDestination(playerPos); if (mainEnemy) anim.SetBool("Walking", true); return; } if (Vector3.Distance(transform.position, agent.destination) <= 5) { int randomIndex = Random.Range(0, navPoints.Count); agent.SetDestination(navPoints[randomIndex].position); if (mainEnemy) anim.SetBool("Walking", true); } } else { agent.SetDestination(playerPos); if (mainEnemy) { if(Vector3.Distance(transform.position, playerPos) <= 1f) { anim.SetBool("Walking", false); agent.isStopped = true; } else { anim.SetBool("Walking", true); } } return; } } private void OnTriggerEnter(Collider other) { if(other.gameObject.name == "FPSController" && gameMgr.GetGameOverState() == false) { gameMgr.OnGameOver(); agent.isStopped = true; } } public void SetAudioSourceState(bool state) { if (state) aSource.Play(); else aSource.Stop(); } public void OnGameReset() { transform.position = startingPos; engagePlayer = false; } public void TargetPlayer() { engagePlayer = true; } }