Is there a way to display navmesh agent path in Unity?
Posted
by
Antoine Guillien
on Game Development
See other posts from Game Development
or by Antoine Guillien
Published on 2013-12-25T11:37:38Z
Indexed on
2014/06/02
16:02 UTC
Read the original article
Hit count: 369
unity
|path-finding
I'm currently making a prototype for a game I plan to develop. As far as I did, I managed to set up the navigation mesh and my navmeshagents.
I would like to display the path they are following when setDestination()
is fired.
I did some researches but didn't find anything about it.
EDIT 1 : So I instantiate an empty object with a LineRenderer and I have a line bewteen my agent and the destination. Still I've not all the points when the path has to avoid an obstacle. Furthermore, I wonder if the agent.path does reflect the real path that the agent take as I noticed that it actually follow a "smoothier" path.
Here is the code so far :
GameObject container = new GameObject();
container.transform.parent = agent.gameObject.transform;
LineRenderer ligne = container.AddComponent<LineRenderer>();
ligne.SetColors(Color.white,Color.white);
ligne.SetWidth(0.1f,0.1f);
//Get def material
ligne.gameObject.renderer.material.color = Color.white;
ligne.gameObject.renderer.material.shader = Shader.Find("Sprites/Default");
ligne.gameObject.AddComponent<LineScript>();
ligne.SetVertexCount(agent.path.corners.Length+1);
int i = 0;
foreach(Vector3 v in p.corners)
{
ligne.SetPosition(i,v);
//Debug.Log("position agent"+g.transform.position);
//Debug.Log("position corner = "+v);
i++;
}
ligne.SetPosition(p.corners.Length,agent.destination);
ligne.gameObject.tag = "ligne";
So How can I get the real coordinates my agent is going to walk throught ?
© Game Development or respective owner