I need help making this code work. It is made for the enemy AI to move and patrol, chase the player and attack the player.
The problem is when it starts to attack the player, then the player can move around the enemy and the enemy will not turn to face the player but just keeps attacking in front of him.
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
public class EnemyAI : MonoBehaviour
{
public float minRandomDistance = 5.0f;
public float maxRandomDistance = 10.0f;
public float patrolSpeed = 2.0f;
public float chaseSpeed = 5.0f;
public float chaseRange = 10.0f;
public float attackRange = 1f;
public float rotationSpeed = 2f;
public Transform player;
private Animator animator;
private NavMeshAgent navAgent;
private Vector3 randomDestination;
private bool isChasing = false;
private bool isAttacking = false;
private string[] attackAnimation = { "isAttacking1", "isAttacking2" };
private void Start()
{
navAgent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
SetRandomDestination();
}
private void Update()
{
if (isChasing)
{
ChasePlayer();
}
else
{
Patrol();
}
FaceMovementDirection();
}
private void Patrol()
{
if (!navAgent.pathPending && navAgent.remainingDistance < 0.5f)
{
SetRandomDestination();
}
// Check if the player is within chase range
if (Vector3.Distance(transform.position, player.position) < chaseRange)
{
isChasing = true;
navAgent.speed = chaseSpeed;
animator.SetBool("isRunning", true);
}
else
{
isChasing = false;
navAgent.speed = patrolSpeed;
animator.SetBool("isRunning", false);
}
if (Vector3.Distance(transform.position, player.position) < attackRange)
{
isAttacking = true;
navAgent.isStopped = true;
}
else
{
isAttacking = false;
navAgent.isStopped = false;
}
foreach (string attackParam in attackAnimation)
{
animator.SetBool(attackParam, isAttacking);
}
}
private void ChasePlayer()
{
// Move towards the player's position
navAgent.SetDestination(player.position);
// Check if the player is out of chase range
if (Vector3.Distance(transform.position, player.position) > chaseRange)
{
isChasing = false;
navAgent.speed = patrolSpeed;
animator.SetBool("isRunning", false);
}
if (Vector3.Distance(transform.position, player.position) < attackRange)
{
isAttacking = true;
navAgent.isStopped = true;
// Rotate to face the player's position
Vector3 directionToPlayer = player.position - transform.position;
directionToPlayer.y = 0;
Quaternion targetRotation = Quaternion.LookRotation(directionToPlayer);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
foreach (string attackParam in attackAnimation)
{
animator.SetBool(attackParam, isAttacking);
}
}
else
{
isAttacking = false;
navAgent.isStopped = false;
}
foreach (string attackParam in attackAnimation)
{
animator.SetBool(attackParam, isAttacking);
}
}
private void SetRandomDestination()
{
// Generate a random direction and distance within the specified range
Vector3 randomDirection = Random.insideUnitSphere;
randomDirection.y = 0; // Keep the movement on the same plane
Vector3 targetPosition = transform.position + randomDirection.normalized * Random.Range(minRandomDistance, maxRandomDistance);
// Set the NavMeshAgent's destination
navAgent.SetDestination(targetPosition);
}
private void FaceMovementDirection()
{
Vector3 moveDirection = navAgent.velocity.normalized;
if (moveDirection.magnitude > 0.1f)
{
Quaternion targetRotation = Quaternion.LookRotation(-moveDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * 500.0f);
}
}
}