Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why is My NavMesh Agent Not Moving?

Discussion in 'Navigation' started by unity_cy57tWKy3cnEnw, Jun 6, 2023.

  1. unity_cy57tWKy3cnEnw

    unity_cy57tWKy3cnEnw

    Joined:
    Mar 11, 2021
    Posts:
    20
    Hello!
    I have an enemy in my scene that functions as a pretty standard "Weeping Angel". It moves toward you when you're looking away, and freezes whenever in sight.

    In my test scene, it works perfectly fine. This scene is composed of a simple plane without anything really stopping the enemy from moving about its path.

    However, I've transferred my enemy into an in-game level, and I'm running into some issues. For the most part, it's just not able to move, and I have no idea why. There have been a few edge cases where it's chased after the player, but it's very difficult to get it to actually happen. I've been messing around with all sorts of values but nothing seems to do anything. The only thing that seems a bit out of the ordinary is that my NavMesh Surface is inclining off of the ground in a few spots seemingly randomly.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class TheCritic : MonoBehaviour
    7. {
    8.     public bool active;
    9.     [SerializeField] private GameObject player;
    10.     [SerializeField] private Transform playerTransform;
    11.     [SerializeField] private Camera playerCam;
    12.     [SerializeField] private Transform enemyTransform;
    13.     [SerializeField] private NavMeshAgent enemyNavMesh;
    14.     [SerializeField] private Renderer enemyRender;
    15.     [SerializeField] private Animator enemyAnim;
    16.     [SerializeField] private float moveDelay;
    17.     [SerializeField] private float moveSpeed;
    18.     [SerializeField] private int damage;
    19.     [SerializeField] private float hitDelay;
    20.     [SerializeField] private float minDistance;
    21.  
    22.     private bool hit;
    23.     [SerializeField] private bool visible;
    24.  
    25.     [SerializeField] private float delayTimer;
    26.     private float hitTimer;
    27.     [SerializeField] private float distance;
    28.  
    29.     private int poseSelect;
    30.     [SerializeField] private bool poseSelected;
    31.  
    32.     [SerializeField] private bool isStopped;
    33.  
    34.     // Start is called before the first frame update
    35.     void Start()
    36.     {
    37.         enemyNavMesh = GetComponent<NavMeshAgent>();
    38.         delayTimer = 1;
    39.         hitTimer = 1;
    40.         enemyRender = enemyRender.GetComponent<Renderer>();
    41.         poseSelect = 0;
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     void Update()
    46.     {
    47.         isStopped = enemyNavMesh.isStopped;
    48.         enemyAnim.SetInteger("Pose", poseSelect);
    49.         distance = Vector3.Distance(playerTransform.position, enemyTransform.position);
    50.         if (active)
    51.         {
    52.             if (enemyRender.isVisible == false && distance > minDistance)
    53.             {
    54.              
    55.                 visible = false;
    56.                 if (delayTimer > 0)
    57.                 {
    58.                     delayTimer -= 1 * Time.deltaTime / moveDelay;
    59.                 }
    60.                 else
    61.                 {
    62.                     if(poseSelected == false)
    63.                     {
    64.                         poseSelect = Random.Range(1, 3);
    65.                         poseSelected = true;
    66.                     }
    67.                     LookAt();
    68.                     Moving();
    69.                 }
    70.             }
    71.             else
    72.             {
    73.                 visible = true;
    74.                 delayTimer = 1;
    75.                 enemyNavMesh.isStopped = true;
    76.             }
    77.  
    78.             if(distance <= minDistance && enemyRender.isVisible == false)
    79.             {
    80.                 Attack();
    81.             }
    82.  
    83.             if(distance > minDistance)
    84.             {
    85.                 hitTimer = 1;
    86.             }
    87.  
    88.             if(enemyRender.isVisible == true)
    89.             {
    90.                 poseSelected = false;
    91.             }
    92.         }
    93.     }
    94.  
    95.     void Moving()
    96.     {
    97.         //enemyNavMesh.destination = playerTransform.position;
    98.         enemyNavMesh.isStopped = false;
    99.         enemyNavMesh.SetDestination(playerTransform.position);
    100.     }
    101.  
    102.     void Attack()
    103.     {
    104.         if (hitTimer > 0)
    105.         {
    106.             hitTimer -= 1 * Time.deltaTime / hitDelay;
    107.         }
    108.         else
    109.         {
    110.             hit = false;
    111.             if(hit == false)
    112.             {
    113.                 player.GetComponent<HealthSystem>().hit = true;
    114.                 player.GetComponent<HealthSystem>().health = player.GetComponent<HealthSystem>().health - damage;
    115.                 hit = true;
    116.             }
    117.             hitTimer = 1;
    118.         }
    119.     }
    120.  
    121.     void LookAt()
    122.     {
    123.         Vector3 lookAtPosition = playerTransform.position;
    124.         lookAtPosition.y = enemyTransform.position.y;
    125.         enemyTransform.LookAt(lookAtPosition);
    126.     }
    127. }
    128.  
    Screenshot (1161) - Copy.png Screenshot (1162) - Copy.png Screenshot (1163).png Screenshot (1161).png Screenshot (1162).png
    Below, I've included a few images of my scene and variables as well as the enemy code. If anyone can offer any solutions I'd love to hear. If there's information you still need, please let me know. Thank you!

    EDIT: The character does rotate and change poses properly, meaning that the code is reaching the point where it tells the enemy to move:)