using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AIController : MonoBehaviour
{
    public Transform player;
    public Transform[] waypoints;
    public float moveSpeed = 5f;
    public float rotationSpeed = 2f;
    public Animator animator;

    private int currentWaypointIndex = 0;
    private bool isPlayerVisible = false;
    private Vector3 lastKnownPlayerPosition;

    private void Start()
    {
        lastKnownPlayerPosition = player.position;
    }

    private void Update()
    {
        if (isPlayerVisible)
        {
            ChasePlayer();
        }
        else
        {
            SearchForPlayer();
        }
    }

    private void SearchForPlayer()
    {
        // Calculate target rotation and move direction towards the last known player position
        Quaternion targetRotation = Quaternion.LookRotation(lastKnownPlayerPosition - transform.position);
        Vector3 moveDirection = lastKnownPlayerPosition - transform.position;

        // Check if there are any obstacles between the AI and the player
        RaycastHit hit;
        if (Physics.Raycast(transform.position, moveDirection, out hit))
        {
            if (!hit.collider.CompareTag("Player"))
            {
                // Smoothly rotate towards the last known player position
                transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

                // Move towards the last known player position
                transform.position += moveDirection.normalized * moveSpeed * Time.deltaTime;

                // Play walking animation
                animator.SetBool("IsWalking", true);
            }
            else
            {
                // Player is visible, switch to chasing mode
                isPlayerVisible = true;
            }
        }

        // If reached the last known player position and player is not visible, wait for 3 seconds and go to a random waypoint
        if (Vector3.Distance(transform.position, lastKnownPlayerPosition) < 1f && !isPlayerVisible)
        {
            StartCoroutine(WaitAndGoToRandomWaypoint(3f));
        }
    }
    
    private IEnumerator WaitAndGoToRandomWaypoint(float waitTime)
    {
        // Play idle animation
        animator.SetBool("IsWalking", false);

        // Wait for the specified time
        yield return new WaitForSeconds(waitTime);

        // Go to a random waypoint
        GoToRandomWaypoint();
    }

    private void GoToRandomWaypoint()
    {
        int randomIndex = Random.Range(0, waypoints.Length);
        currentWaypointIndex = randomIndex;

        // Calculate target rotation and move direction towards the random waypoint
        Quaternion targetRotation = Quaternion.LookRotation(waypoints[currentWaypointIndex].position - transform.position);
        Vector3 moveDirection = waypoints[currentWaypointIndex].position - transform.position;

        // Move towards the random waypoint
        transform.position += moveDirection.normalized * moveSpeed * Time.deltaTime;

        // Smoothly rotate towards the random waypoint
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

        // Play walking animation
        animator.SetBool("IsWalking", true);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            isPlayerVisible = true;
            lastKnownPlayerPosition = player.position;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            isPlayerVisible = false;
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (!isPlayerVisible && collision.gameObject.CompareTag("Obstacle"))
        {
            // If there is an obstacle in front of the AI, go to a new random waypoint
            GoToRandomWaypoint();
        }
    }
}

Fix Bugs In Code