i3e-asg2/Assets/Scripts/EnemyAi.cs
Sc0rch-thinks b0364a3dbf final commit yay
commented abit then built the game
2024-07-05 23:30:15 +08:00

192 lines
5.2 KiB
C#

/*
*Author: Lin Hengrui Ryan
*Date:21/06/2024
*Description: a sript to make enemy move
*/
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
public class EnemyAi : MonoBehaviour
{
/// <summary>
/// the navmesh agen to allow the enemy know where it can navigate
/// </summary>
public NavMeshAgent agent;
/// <summary>
/// to refrence the player to navigate the enemy to the player
/// </summary>
public Transform player;
/// <summary>
/// the physics layer of the ground so that the enemy knows where he can walk
/// </summary>
public LayerMask groundMask;
/// <summary>
/// the physic layer of the player to allow the enemy to checksphere
/// </summary>
public LayerMask playerMask;
/// <summary>
/// the health of the enemy
/// </summary>
public int health = 100;
[Header("enemy stats")]
/// <summary>
/// the point that the enemy will walk to when they are patroling
/// </summary>
private Vector3 WalkPoint;
/// <summary>
/// to bool to allow the walkpoint to be reset
/// </summary>
private bool walkPointSet = false;
/// <summary>
/// the maximum range that the enemy will walk while patroling
/// </summary>
public float walkRange;
/// <summary>
/// how far the player is from the enemy to start chasing the player
/// </summary>
public float sightRange;
/// <summary>
/// how far the player is from the enemy to allow the enemy to start shooting
/// </summary>
public float attackRange;
/// <summary>
/// a bool to call the chase function
/// </summary>
private bool playerInSightRange;
/// <summary>
/// a bool to call the shoot function
/// </summary>
private bool playerInAttackRange;
/// <summary>
/// a variable to allow the enemy to refrence the gun to call its functions
/// </summary>
public GameObject gun;
/// <summary>
/// to show the enemy health in the world space ui
/// </summary>
private Slider healthbar;
/// <summary>
/// to calibrate the enemy health on load
/// </summary>
void Awake()
{
healthbar = GetComponentInChildren<Slider>();
healthbar.maxValue = health;
}
/// <summary>
/// to constantly check for the player in the sphere and do the nessasary action and to destroy the enemy dies after the heath reaches 0 or less
/// </summary>
void Update()
{
//Check for sight and attack range
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, playerMask);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, playerMask);
if (!playerInSightRange && !playerInAttackRange)
Patrol();
if (playerInSightRange && !playerInAttackRange)
Chase();
if (playerInAttackRange && playerInSightRange)
Attack();
if (health <= 0)
{
Destroy(gameObject);
}
if (player == null)
{
player = GameObject.Find("PlayerCapsule").transform;
}
healthbar.value = health;
}
/// <summary>
/// to set the enemy to go to a position specified
/// </summary>
private void Patrol()
{
Debug.Log("Patroling");
if (!walkPointSet)
{
SeachWalkPoint();
}
else
{
agent.SetDestination(WalkPoint);
}
;
Vector3 distaceToWalk = transform.position - WalkPoint;
if (distaceToWalk.magnitude < 1f)
{
Invoke("changePoint", 4f);
}
}
/// <summary>
/// to allow the walkpoint to change
/// </summary>
public void changePoint()
{
walkPointSet = false;
}
/// <summary>
/// seach for a new walk point to somewhere that is in the walk radius and is on the ground
/// </summary>
private void SeachWalkPoint()
{
float randomZ = Random.Range(-walkRange, walkRange);
float randomX = Random.Range(-walkRange, walkRange);
WalkPoint = new Vector3(
transform.position.x + randomX,
transform.position.y,
transform.position.z + randomZ
);
if (Physics.Raycast(WalkPoint, -transform.up, 2f, groundMask))
{
walkPointSet = true;
}
}
/// <summary>
/// to make the enemy chase the player if it is not in the shooting range
/// </summary>
private void Chase()
{
Debug.Log("chasing");
agent.SetDestination(player.position);
}
/// <summary>
/// to make the enemy attack the player
/// </summary>
private void Attack()
{
Debug.Log("Attacking");
agent.SetDestination(transform.position);
transform.LookAt(player);
gun.GetComponent<enemyGun>().Shoot();
}
/// <summary>
/// to see the ranges in the scene view
/// </summary>
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, sightRange);
}
}