Search Unity

having issues with NavMesh

Discussion in 'Scripting' started by BatgirlDee, May 20, 2019.

  1. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    hi there i have already tried almost everything i can to sort out the issue im having but it still keeps happening

    i keep getting this error :
    Failed to create agent because it is not close enough to the NavMesh
    UnityEngine.Object:Instantiate(Spector)

    the partof the script it keeps refuring to below;

    Code (CSharp):
    1.  void Spector()
    2.     {
    3.         SpectorInstance = Instantiate(SpectorPrefab) /*as Spector*/;
    4.         SpectorInstance.SetLocation(mazeInstance.GetCell(mazeInstance.RandomCoordinates));
    5.  
    6.     }
    the code for the Sprctor(nevmash agent)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class Spector : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField]
    10.    public GameObject _destination;
    11.  
    12.      NavMeshAgent _navMeshagent;
    13.  
    14.     private MazeCell currentCell;
    15.  
    16.     private MazeDirection currentDirection;
    17.  
    18.     public void SetLocation(MazeCell cell)
    19.     {
    20.         if (currentCell != null)
    21.         {
    22.             currentCell.OnPlayerExited();
    23.         }
    24.         currentCell = cell;
    25.         transform.localPosition = cell.transform.localPosition;
    26.         currentCell.OnPlayerEntered();
    27.     }
    28.  
    29.     // Use this for initialization
    30.     public void Start()
    31.     {
    32.         _navMeshagent = this.GetComponent<NavMeshAgent>();
    33.  
    34.         _destination = GameObject.FindWithTag("Player");      
    35.  
    36.         if (_navMeshagent == null)
    37.         {
    38.             Debug.LogError("Nav Mesh Agent component not found attached to " + gameObject.name);
    39.         }
    40.         else
    41.         {
    42.             SetDestination();
    43.         }
    44.     }
    45.  
    46.     private void SetDestination()
    47.     {
    48.  
    49.         NavMeshHit closestHit;
    50.  
    51.         if (NavMesh.SamplePosition(transform.position, out closestHit, 100f, NavMesh.AllAreas))
    52.         {
    53.             transform.position = closestHit.position;
    54.         }
    55.         if (Time.time > .1f)
    56.         {
    57.             GetComponent<NavMeshAgent>().enabled = true;
    58.         }
    59.         if (_destination != null)
    60.         {
    61.             Vector3 targetVector = _destination.transform.position;
    62.             _navMeshagent.SetDestination(targetVector);
    63.         }
    64.     }
    65. }
    any help would be great and thanks in advance
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Can you visualize where are you trying to put your agent?

    Add this code between lines 52 and 53:

    Code (CSharp):
    1. GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = closestHit.position;
    This will create a sphere and place it where you trying to place your agent. After adding the code, open navigation tab to make navmesh visible and then you'll see where it really is. This is how I was debugging such errors.
     
  3. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    i ve added it an all it does it create a sphere under the agent
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Exactly. Have you navigation tab opened? Is the sphere on the navmesh or aside from it?
     
  5. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    its on it
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Can your Spector move over the area where spere is created? Have your tried using _navMeshAgent.Warp(position) instead of setting transform.position value directly?
     
  7. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36
    I've rewrote the script many different ways added extra bits and removed some bits but still throws the same error
     
  8. BatgirlDee

    BatgirlDee

    Joined:
    Mar 20, 2019
    Posts:
    36