Hi folks, I’m having an issue drawing a Navmashagent path. I have a gameobject (with a navmeshagent component attached) and the following script. The issue is strange - the agent does manage to navigate obstacles in the scene correctly, but for some reason it’s not drawing the path correctly.
The reason for this is the line:
m_agent.SetDestination (targetPosition);
For some reason (even though it’s pathfinding is correct), this line only generates one corner (even in a straight line - where there should be two points). Can’t figure out why the navigation is working ,but the corners array is only showing one item in the array. Any help would be appreciated!
using UnityEngine;
using System.Collections;
public class SimpleNavMeshMoveTo : MonoBehaviour {
public Transform goal;
NavMeshAgent m_agent;
private LineRenderer m_line;
public Transform m_home;
public int m_numberOfCorners;
////////////////////////////////////////////////////////////////////////////////////////////////
void Start () {
m_agent = GetComponent<NavMeshAgent> ();
m_line = GetComponent<LineRenderer> ();
}
////////////////////////////////////////////////////////////////////////////////////////////////
// If button is pushed down, go to target, when release retreat home
void Update()
{
if (Input.GetMouseButtonDown (1)) {
RaycastHit hit;
if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 100.0f)) {
StartCoroutine ( UpdatePath (hit.point) );
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
IEnumerator UpdatePath( Vector3 targetPosition )
{
// m_agent.path.ClearCorners ();
m_line.SetPosition (0, transform.position);
m_agent.SetDestination (targetPosition);
while (m_agent.pathPending) {
yield return null;
}
//yield return new WaitForEndOfFrame ();
// This is in a coroutine as we need to wait for end of frame to ensure path is generates
DrawPath ( m_agent.path );
}
////////////////////////////////////////////////////////////////////////////////////////////////
void DrawPath ( NavMeshPath path )
{
m_numberOfCorners = path.corners.Length;
Debug.Log ("DrawPath Start. Number Corners: "+path.corners.Length);
// Check is path as 1 or no corers
if ( path.corners.Length <2 ) return;
m_line.SetVertexCount (path.corners.Length);
for (int index = 0; index < path.corners.Length; index++) {
m_line.SetPosition (index, path.corners [index]);
}
Debug.Log ("DrawPath End");
}
}