Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trouble with NavMesh AI.

Discussion in 'Scripting' started by KirbyII, Jan 22, 2023.

  1. KirbyII

    KirbyII

    Joined:
    Apr 1, 2022
    Posts:
    2
    I've been trying to create an enemy AI with navMesh so the enemy can follow the player when the player enters the enemies radius. however it's given me errors when I play test and wont work at all, so I'm at a loss.

    i have a navMesh set to the ground which is a terrain that also has a terrain collider which is set to static. The enemy has a navMeshAgent and the enemy and the agent are touching the ground and the navMesh.

    when i play test it come up with these errors:

    Failed to create agent because it is not close enough to the NavMesh
    and
    "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.AI.NavMeshAgent:SetDestination (UnityEngine.Vector3)
    EnemyAI:Update () (at Assets/Enemies/EnemyAI.cs:25)

    i don't know what the first one means cause the agent is touching the navMesh so its a bit confusing.

    here is the script that runs it;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class EnemyAI : MonoBehaviour
    7. {
    8.     public float visionRadius = 10f;
    9.  
    10.     Transform target;
    11.     NavMeshAgent agent;
    12.  
    13.     void Start()
    14.     {
    15.         target = playerManager.instance.player.transform;
    16.         agent = GetComponent<NavMeshAgent>();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         float distance = Vector3.Distance(target.position, transform.position);
    22.  
    23.         if(distance <= visionRadius)
    24.         {
    25.             agent.SetDestination(target.position);
    26.  
    27.             if(distance <= agent.stoppingDistance)
    28.             {
    29.                 FaceTarget();
    30.             }
    31.         }
    32.     }
    33.  
    34.     void FaceTarget()
    35.     {
    36.         Vector3 direction = (target.position - transform.position).normalized;
    37.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    38.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    39.     }
    40.  
    41.     void OnDrawGizmosSelected()
    42.     {
    43.         Gizmos.color = Color.red;
    44.         Gizmos.DrawWireSphere(transform.position, visionRadius);
    45.     }
    46. }
    47.  
    any help to find what I'm missing would be much appreciated.
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    What version of Unity are you using?

    Navmesh does not depend on physics collision, thus a terrain collider is of no help. NavMesh generates its own thing called a "NavMesh Surface", which needs to be baked. In older versions, the NavMesh Surface is not within the scene, while in the latest version, it is a component to be placed in the scene.

    Those errors are likely because you do not have a NavMesh surface, and therefore your NavMesh agent has no idea where to go.