2024-07-05 15:30:15 +00:00
|
|
|
/*
|
|
|
|
*Author: Lin Hengrui Ryan
|
|
|
|
*Date:21/06/2024
|
|
|
|
*Description: a sript to make enemy move
|
|
|
|
*/
|
2024-07-02 02:42:09 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
2024-07-04 17:22:55 +00:00
|
|
|
using UnityEngine.UI;
|
|
|
|
|
2024-07-02 02:42:09 +00:00
|
|
|
public class EnemyAi : MonoBehaviour
|
|
|
|
{
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// the navmesh agen to allow the enemy know where it can navigate
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public NavMeshAgent agent;
|
2024-07-05 15:30:15 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// to refrence the player to navigate the enemy to the player
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public Transform player;
|
2024-07-05 15:30:15 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// the physics layer of the ground so that the enemy knows where he can walk
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public LayerMask groundMask;
|
2024-07-05 15:30:15 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// the physic layer of the player to allow the enemy to checksphere
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public LayerMask playerMask;
|
2024-07-05 15:30:15 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// the health of the enemy
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public int health = 100;
|
|
|
|
|
2024-07-05 15:30:15 +00:00
|
|
|
[Header("enemy stats")]
|
2024-07-02 02:42:09 +00:00
|
|
|
/// <summary>
|
2024-07-05 15:30:15 +00:00
|
|
|
/// 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
|
2024-07-02 02:42:09 +00:00
|
|
|
/// </summary>
|
|
|
|
private bool walkPointSet = false;
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// the maximum range that the enemy will walk while patroling
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public float walkRange;
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// how far the player is from the enemy to start chasing the player
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public float sightRange;
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// how far the player is from the enemy to allow the enemy to start shooting
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public float attackRange;
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// a bool to call the chase function
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
private bool playerInSightRange;
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// a bool to call the shoot function
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
private bool playerInAttackRange;
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// a variable to allow the enemy to refrence the gun to call its functions
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
public GameObject gun;
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// to show the enemy health in the world space ui
|
|
|
|
/// </summary>
|
2024-07-04 17:22:55 +00:00
|
|
|
private Slider healthbar;
|
2024-07-02 02:42:09 +00:00
|
|
|
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <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>
|
2024-07-02 02:42:09 +00:00
|
|
|
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);
|
|
|
|
}
|
2024-07-05 15:30:15 +00:00
|
|
|
if (player == null)
|
2024-07-03 12:03:50 +00:00
|
|
|
{
|
2024-07-05 15:30:15 +00:00
|
|
|
player = GameObject.Find("PlayerCapsule").transform;
|
2024-07-03 12:03:50 +00:00
|
|
|
}
|
2024-07-05 15:30:15 +00:00
|
|
|
healthbar.value = health;
|
2024-07-02 02:42:09 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// to set the enemy to go to a position specified
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
private void Patrol()
|
|
|
|
{
|
|
|
|
Debug.Log("Patroling");
|
|
|
|
if (!walkPointSet)
|
|
|
|
{
|
|
|
|
SeachWalkPoint();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
agent.SetDestination(WalkPoint);
|
|
|
|
}
|
|
|
|
;
|
|
|
|
Vector3 distaceToWalk = transform.position - WalkPoint;
|
|
|
|
if (distaceToWalk.magnitude < 1f)
|
2024-07-03 12:03:50 +00:00
|
|
|
{
|
2024-07-05 15:30:15 +00:00
|
|
|
Invoke("changePoint", 4f);
|
2024-07-03 12:03:50 +00:00
|
|
|
}
|
2024-07-02 02:42:09 +00:00
|
|
|
}
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// to allow the walkpoint to change
|
|
|
|
/// </summary>
|
2024-07-03 12:03:50 +00:00
|
|
|
public void changePoint()
|
|
|
|
{
|
2024-07-05 15:30:15 +00:00
|
|
|
walkPointSet = false;
|
2024-07-03 12:03:50 +00:00
|
|
|
}
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// seach for a new walk point to somewhere that is in the walk radius and is on the ground
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// to make the enemy chase the player if it is not in the shooting range
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
private void Chase()
|
|
|
|
{
|
|
|
|
Debug.Log("chasing");
|
|
|
|
|
|
|
|
agent.SetDestination(player.position);
|
|
|
|
}
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// to make the enemy attack the player
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
private void Attack()
|
|
|
|
{
|
|
|
|
Debug.Log("Attacking");
|
|
|
|
|
|
|
|
agent.SetDestination(transform.position);
|
|
|
|
|
|
|
|
transform.LookAt(player);
|
|
|
|
|
|
|
|
gun.GetComponent<enemyGun>().Shoot();
|
|
|
|
}
|
2024-07-05 15:30:15 +00:00
|
|
|
/// <summary>
|
|
|
|
/// to see the ranges in the scene view
|
|
|
|
/// </summary>
|
2024-07-02 02:42:09 +00:00
|
|
|
private void OnDrawGizmosSelected()
|
|
|
|
{
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
Gizmos.DrawWireSphere(transform.position, attackRange);
|
|
|
|
Gizmos.color = Color.yellow;
|
|
|
|
Gizmos.DrawWireSphere(transform.position, sightRange);
|
|
|
|
}
|
|
|
|
}
|