Search Unity

I need help with optimizing the AI!

Discussion in 'Navigation' started by FunniDino, Mar 5, 2022.

  1. FunniDino

    FunniDino

    Joined:
    Dec 5, 2021
    Posts:
    1
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class zombie_AI : MonoBehaviour
    7. {
    8.     [Header("References")]
    9.     public NavMeshAgent AI;
    10.     public Animator anim;
    11.     public FieldOfView fov;
    12.     [Header("Values")]
    13.     [SerializeField] float roamingSpeed;
    14.     [SerializeField] float targetingSpeed;
    15.     [SerializeField] public float Health = 100f;
    16.     [Header("Booleans")]
    17.     [SerializeField] bool playerDetected = false;
    18.     [Header("Audio")]
    19.     [SerializeField] AudioSource audioSource;
    20.     [SerializeField] AudioClip growl;
    21.  
    22.     Vector3 finalPosition;
    23.     private Transform target = null;
    24.     bool canAttackAgain = true;
    25.     bool canAttack = false;
    26.     bool isStoppedCoroutine = false;
    27.  
    28.     // Start is called before the first frame update
    29.     void Start()
    30.     {
    31.         StartCoroutine(Roaming());
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.         if(Health > 0)
    38.         {
    39.             Intelligence();
    40.             if (!audioSource.isPlaying && fov.canSeePlayer)
    41.             {
    42.                 audioSource.PlayOneShot(growl);
    43.             }
    44.             else if(audioSource.isPlaying && !fov.canSeePlayer)
    45.             {
    46.                 audioSource.Stop();
    47.             }
    48.         }
    49.         else
    50.         {
    51.             if(audioSource.isPlaying)
    52.             {
    53.                 StopAllCoroutines();
    54.                 audioSource.Stop();
    55.             }
    56.             StartCoroutine(stopMoving());
    57.             Invoke("death", 10);
    58.             anim.SetBool("isTargeting", false);
    59.             anim.SetBool("isRoaming", false);
    60.             anim.SetTrigger("Death");
    61.         }
    62.     }
    63.  
    64.     public void Intelligence()
    65.     {
    66.         if(canAttack && canAttackAgain && fov.canSeePlayer)
    67.         {
    68.             StartCoroutine(doAttack());
    69.         }
    70.  
    71.         if (fov.canSeePlayer)
    72.         {
    73.             playerDetected = true;
    74.             if (anim.GetBool("isTargeting") == false)
    75.             {
    76.                 AI.speed = targetingSpeed;
    77.                 anim.SetBool("isRoaming", false);
    78.                 anim.SetBool("isTargeting", true);
    79.             }
    80.             AI.isStopped = false;
    81.             if(isStoppedCoroutine)
    82.             {
    83.                 StopCoroutine(stopMoving());
    84.             }
    85.             AI.SetDestination(PlayerMove.instance.gameObject.transform.position);
    86.         }
    87.         else
    88.         {
    89.             if (AI.remainingDistance <= AI.stoppingDistance)
    90.             {
    91.                 if (!AI.hasPath || Mathf.Abs(AI.velocity.sqrMagnitude) < float.Epsilon)
    92.                 {
    93.                     isStoppedCoroutine = true;
    94.                     anim.SetBool("isRoaming", false);
    95.                 }
    96.             }
    97.  
    98.  
    99.             playerDetected = false;
    100.             if (anim.GetBool("isRoaming") == false)
    101.             {
    102.                 StartCoroutine(stopMoving());
    103.             }
    104.             else
    105.             {
    106.                 StopCoroutine(stopMoving());
    107.             }
    108.             anim.SetBool("isTargeting", false);
    109.         }
    110.     }
    111.  
    112.     IEnumerator stopMoving()
    113.     {
    114.         isStoppedCoroutine = true;
    115.         AI.velocity = Vector3.zero;
    116.         AI.isStopped = true;
    117.         yield return new WaitForSeconds(6000);
    118.     }
    119.  
    120.     public void death()
    121.     {
    122.         Destroy(gameObject);
    123.     }
    124.  
    125.     IEnumerator Roaming()
    126.     {
    127.         yield return new WaitForSeconds(Random.Range(10, 20));
    128.         isStoppedCoroutine = true;
    129.         if (!playerDetected && Health > 0 && !canAttack && anim.GetBool("isRoaming") == false)
    130.         {
    131.             AI.speed = roamingSpeed;
    132.             //randomize the path
    133.             anim.SetBool("isRoaming", true);
    134.             AI.isStopped = false;
    135.             Vector3 randomDirection = Random.insideUnitSphere * 20;
    136.             randomDirection += transform.position;
    137.             NavMeshHit hit;
    138.             NavMesh.SamplePosition(randomDirection, out hit, 20, 1);
    139.             finalPosition = hit.position;
    140.             Debug.Log(finalPosition);
    141.             if(AI.CalculatePath(finalPosition, AI.path) && AI.path.status == NavMeshPathStatus.PathComplete)
    142.             {
    143.                 AI.SetDestination(finalPosition);
    144.             }
    145.             else
    146.             {
    147.                 isStoppedCoroutine = true;
    148.             }
    149.             StartCoroutine(Roaming());
    150.         }
    151.         else
    152.         {
    153.             //resets the couroutine...
    154.             StartCoroutine(Roaming());
    155.         }
    156.     }
    157.  
    158.     IEnumerator doAttack()
    159.     {
    160.         if(canAttackAgain)
    161.         {
    162.             canAttackAgain = false;
    163.             yield return new WaitForSeconds(2f);
    164.             if(canAttack)
    165.             {
    166.                 PlayerMove.instance.Hurt(20);
    167.             }
    168.             canAttackAgain = true;
    169.             StopCoroutine(doAttack());
    170.         }
    171.     }
    172.  
    173.  
    174.     private void OnTriggerEnter(Collider other)
    175.     {
    176.         if (other.tag == "Player")
    177.         {
    178.             canAttack = true;
    179.         }
    180.     }
    181.  
    182.     private void OnTriggerExit(Collider other)
    183.     {
    184.         if (other.tag == "Player")
    185.         {
    186.             canAttack = false;
    187.         }
    188.     }
    189. }
    190.  
    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
     

    Attached Files:

  2. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    Framerate doesn't tell anything. Please look at your profiler timeline (deep profile) instead and see which functions take most of the time.