I need help with optimizing the AI!

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

public class zombie_AI : MonoBehaviour
{
    [Header("References")]
    public NavMeshAgent AI;
    public Animator anim;
    public FieldOfView fov;
    [Header("Values")]
    [SerializeField] float roamingSpeed;
    [SerializeField] float targetingSpeed;
    [SerializeField] public float Health = 100f;
    [Header("Booleans")]
    [SerializeField] bool playerDetected = false;
    [Header("Audio")]
    [SerializeField] AudioSource audioSource;
    [SerializeField] AudioClip growl;

    Vector3 finalPosition;
    private Transform target = null;
    bool canAttackAgain = true;
    bool canAttack = false;
    bool isStoppedCoroutine = false;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Roaming());
    }

    // Update is called once per frame
    void Update()
    {
        if(Health > 0)
        {
            Intelligence();
            if (!audioSource.isPlaying && fov.canSeePlayer)
            {
                audioSource.PlayOneShot(growl);
            }
            else if(audioSource.isPlaying && !fov.canSeePlayer)
            {
                audioSource.Stop();
            }
        }
        else
        {
            if(audioSource.isPlaying)
            {
                StopAllCoroutines();
                audioSource.Stop();
            }
            StartCoroutine(stopMoving());
            Invoke("death", 10);
            anim.SetBool("isTargeting", false);
            anim.SetBool("isRoaming", false);
            anim.SetTrigger("Death");
        }
    }

    public void Intelligence()
    {
        if(canAttack && canAttackAgain && fov.canSeePlayer)
        {
            StartCoroutine(doAttack());
        }

        if (fov.canSeePlayer)
        {
            playerDetected = true;
            if (anim.GetBool("isTargeting") == false)
            {
                AI.speed = targetingSpeed;
                anim.SetBool("isRoaming", false);
                anim.SetBool("isTargeting", true);
            }
            AI.isStopped = false;
            if(isStoppedCoroutine)
            {
                StopCoroutine(stopMoving());
            }
            AI.SetDestination(PlayerMove.instance.gameObject.transform.position);
        }
        else
        {
            if (AI.remainingDistance <= AI.stoppingDistance)
            {
                if (!AI.hasPath || Mathf.Abs(AI.velocity.sqrMagnitude) < float.Epsilon)
                {
                    isStoppedCoroutine = true;
                    anim.SetBool("isRoaming", false);
                }
            }


            playerDetected = false;
            if (anim.GetBool("isRoaming") == false)
            {
                StartCoroutine(stopMoving());
            }
            else
            {
                StopCoroutine(stopMoving());
            }
            anim.SetBool("isTargeting", false);
        }
    }

    IEnumerator stopMoving()
    {
        isStoppedCoroutine = true;
        AI.velocity = Vector3.zero;
        AI.isStopped = true;
        yield return new WaitForSeconds(6000);
    }

    public void death()
    {
        Destroy(gameObject);
    }

    IEnumerator Roaming()
    {
        yield return new WaitForSeconds(Random.Range(10, 20));
        isStoppedCoroutine = true;
        if (!playerDetected && Health > 0 && !canAttack && anim.GetBool("isRoaming") == false)
        {
            AI.speed = roamingSpeed;
            //randomize the path
            anim.SetBool("isRoaming", true);
            AI.isStopped = false;
            Vector3 randomDirection = Random.insideUnitSphere * 20;
            randomDirection += transform.position;
            NavMeshHit hit;
            NavMesh.SamplePosition(randomDirection, out hit, 20, 1);
            finalPosition = hit.position;
            Debug.Log(finalPosition);
            if(AI.CalculatePath(finalPosition, AI.path) && AI.path.status == NavMeshPathStatus.PathComplete)
            {
                AI.SetDestination(finalPosition);
            }
            else
            {
                isStoppedCoroutine = true;
            }
            StartCoroutine(Roaming());
        }
        else
        {
            //resets the couroutine...
            StartCoroutine(Roaming());
        }
    }

    IEnumerator doAttack()
    {
        if(canAttackAgain)
        {
            canAttackAgain = false;
            yield return new WaitForSeconds(2f);
            if(canAttack)
            {
                PlayerMove.instance.Hurt(20);
            }
            canAttackAgain = true;
            StopCoroutine(doAttack());
        }
    }


    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            canAttack = true;
        }
    }

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

Whenever I use the zombie_AI, the framerate goes from nearly 1000fps, to 800fps, and then when it begins roaming it drops to 600fps…

Any advice would really be appreciated

7942828–1016161–zombie_AI.cs (5.09 KB)

Framerate doesn’t tell anything. Please look at your profiler timeline (deep profile) instead and see which functions take most of the time.