/* * author: mark joshwel * date: 19/6/2024 * description: enemy AI based off */ using UnityEngine; using UnityEngine.AI; /// /// AI patrolling, chasing and capturing behaviour for the enemy /// public class HerController : MonoBehaviour { /// /// cache variable for the walking parameter in the animator /// private static readonly int IsMoving = Animator.StringToHash("isMoving"); /// /// cache variable for the chasing parameter in the animator /// private static readonly int IsChasing = Animator.StringToHash("isChasing"); /// /// variable for the nav mesh agent that determines where the enemy can move /// public NavMeshAgent agent; /// /// variable for the player's position /// public Transform player; /// /// variables to distinguish player for sensing /// public LayerMask playerLayerMask; /// /// variable specifying the capture range of the enemy /// public float captureRange; /// /// boolean variable for the player being in attack range /// public bool playerInCaptureRange; /// /// variable for the animator /// private Animator _anim; /// /// variable to store game manager /// private GameManager _game; /// /// function to set any initial values /// private void Awake() { // get the player player = GameObject.FindGameObjectWithTag("Player").transform; // get the nav mesh agent agent = GetComponent(); // get the animator _anim = GetComponent(); // get the game manager _game = GameManager.Instance; } /// /// function to update the enemy's behaviour /// private void Update() { // check for sight and attack range playerInCaptureRange = Physics.CheckSphere(transform.position, captureRange, playerLayerMask); if (!playerInCaptureRange) ChasePlayer(); else CapturePlayer(); } /// /// function to draw/visualize the sight and attack range of the enemy /// private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, captureRange); } /// /// function to chase the player /// private void ChasePlayer() { // start chasing animation _anim.SetBool(IsMoving, true); _anim.SetBool(IsChasing, true); // aim at the player agent.SetDestination(player.position); } /// /// function that captures the player and signals the game manager appropriately /// private void CapturePlayer() { // signal the game manager to show the 'caught!' menu if (_game.Paused) return; Debug.Log("captured..."); _game.SetDisplayState(GameManager.DisplayState.ScreenCaughtMenu); // // stop any moving animations // _anim.SetBool(IsChasing, false); // _anim.SetBool(IsMoving, false); // // don't move the enemy // agent.SetDestination(transform.position); // // look at me! // transform.LookAt(player); } }