Hello everyone,
I am having some trouble with my NavMesh. I have an object that I am trying to get to path find, however, I get this error as soon as my game starts “Failed to create agent because it is not close enough to the NavMesh”. But it is directly on top of the navmesh ( I tried to upload a picture, but it said the file size is too large). I have tried deleting the agent component from the object and adding it in the script right before he moves, but when I do this, he doesn’t move. I have also tried creating the agent component in the start method, but this causes the object to warp to a different spot on the map. I am at a complete loss and would really appreciate any help. This is the code I am trying to use to make him move. when I tried to use the pathfinding I comment out the MoveTowards function.
public bool Move(Vector3 pos, float speed, GameObject unit)
{
bool isMoving = true;
if(unit.transform.position == targetPosition)
{
isMoving = false;
return (isMoving);
}
else
{
unit.transform.position = Vector3.MoveTowards(unit.transform.position, targetPosition, speed * Time.deltaTime);
//unit.GetComponent<NavMeshAgent>().speed = speed * Time.deltaTime;
//unit.GetComponent<NavMeshAgent>().SetDestination(targetPosition);
return (isMoving);
}
}
I had that error when I was using a Prefab. Unity was checking the Prefab and the Prefab wasn't on the NavMesh. You can test this by setting a public value and then checking in the inspector whether your object in the scene changes that value or the prefab changes that value. I think the solution was to instantiate the object with a reference - and then use that reference like you're using "unit".
– Janette5Here is an example - when I call the MoveNow() function from another script the agent will start moving towards the destination. public Transform goal; NavMeshAgent unit; private void Start() { unit = GetComponent<NavMeshAgent>(); } public void MoveNow() { unit.destination = goal.position; }
– Janette5