- Home /
Problem with calculated path of a NavMeshAgent
Hey guys,
I'm currently working on a script to draw the calculated path of an NavMeshAgent. Therefore I use a LineRenderer. The corners of the calculated path are taken to set the points of the LineRenderer.
The problem: The agent always reaches its destination, but depending on the destination the path is not always drawn correctly. In my case the agent should move from one room in a building to another (I took a sphere as destination). In the first situation the agent and the destination are on the same storey and the path is shown correctly. In the next situation I set the destination one storey below and the path is drawn just at the start. Nevertheless the agent moves to the sphere. See the pictures below. While debugging, I could see that the path (path.corners.Length) only has two corners when shown incorrectly (and more when shown correctly). This would explain the short LineRenderer in the second picture, because the LineRenderer is generated out of the corners of the path, so it should always be identical. But why does the agent then reach its target? This would mean the stored path (agent.path) differs from the one the agent actually takes.
public Transform goal;
public LineRenderer line;
public NavMeshAgent agent;
public GameObject LR;
void Start()
{
Debug.Log("Start!");
LR = this.gameObject.transform.GetChild(0).gameObject;
line = LR.AddComponent<LineRenderer>();
line.SetWidth(0.3f, 0.3f);
agent = GetComponent<NavMeshAgent>();
Debug.Log("Agent erstellt");
//Coroutine = GetPath();
StartCoroutine(GetPath());
//GetPath();
}
IEnumerator GetPath()
{
Debug.Log("GetPath gestartet!");
//yield return new WaitForSecondsRealtime(5);
Debug.Log(transform.position);
line.positionCount = 1;
line.SetPosition(0, transform.position); //set the line's origin
agent.SetDestination(goal.position);
//agent.CalculatePath(goal.position);
Debug.Log("Dem Agenten ein Ziel zugewiesen");
Debug.Log("Anzahl der Linienpunkte (Pfad): " + agent.path.corners.Length);
yield return new WaitForEndOfFrame(); //wait for the path to generate
Debug.Log("Nach EndOfFrame: Anzahl der Linienpunkte (Pfad): " + agent.path.corners.Length);
DrawPath(agent.path);
Debug.Log("Ende von GetPath!");
//throw new NotImplementedException();
}
void Update()
{
//StartCoroutine(GetPath());
}
private void OnDrawGizmos()
{
// Draw a yellow sphere at the transform's position
//Gizmos.color = Color.yellow;
//Gizmos.DrawSphere(transform.position, 1);
}
void DrawPath(NavMeshPath path)
{
Debug.Log("DrawPath gestartet!");
if (path.corners.Length < 2) //if the path has 1 or no corners, there is no need
{
Debug.Log("DrawPath.Return!");
return;
}
line.positionCount = path.corners.Length; //set the array of positions to the amount of corners
Debug.Log("Anzahl der Linienpunkte (Pfad): " + path.corners.Length);
Debug.Log("Anzahl der Linienpunkte (Line): " + line.positionCount);
for (var i = 1; i < path.corners.Length; i++)
{
line.SetPosition(i, path.corners[i]); //go through each corner and set that to the line renderer's position
Debug.Log(i + ", " + line.GetPosition(i));
}
Debug.Log("Ende von DrawPath!");
}