- Home /
Draw path along Navmesh agent path
Hi,
Now I have created Navmesh agent, it can walk through intelligently, but now I wanna visualize the path which agent walk along. That is draw a path of navmesh walking in script. Does someone have idea?? Thanks a lot :)
Answer by TakuanDaikon · Oct 04, 2014 at 11:39 PM
This is a very basic example. It can be easily optimized, but I didn't bother because I only needed a quick and dirty visualization:
void OnDrawGizmosSelected()
{
var nav = GetComponent<NavMeshAgent>();
if( nav == null || nav.path == null )
return;
var line = this.GetComponent<LineRenderer>();
if( line == null )
{
line = this.gameObject.AddComponent<LineRenderer>();
line.material = new Material( Shader.Find( "Sprites/Default" ) ) { color = Color.yellow };
line.SetWidth( 0.5f, 0.5f );
line.SetColors( Color.yellow, Color.yellow );
}
var path = nav.path;
line.SetVertexCount( path.corners.Length );
for( int i = 0; i < path.corners.Length; i++ )
{
line.SetPosition( i, path.corners[ i ] );
}
}
Since this has been resurrected already...
If you only need it for debugging this can be done easier than that.
for (int i = 0; i < path.corners.Length - 1; i++)
{
Debug.DrawLine(path.corners[i], path.corners[i + 1], Color.red);
}
True, although the LineRenderer supplied some styling options I preferred that Debug.DrawLine() didn't, which of course I removed before posting the code.
Your way is indeed simple, effective, and concise.
You should promote the comment to an answer :)
This is great thanks ! but It only works when I click on the agent I have the script on in the hierarchy tab.
Right, because it's OnDrawGizmosSelected(). You can rename it to OnDrawGizmos() if you want it displayed all the time ins$$anonymous$$d.
hey man can i restrict two navmesh agents to follow two specific paths? if possible please share a piece of code then
How to leave the line permanently and not make it disappear as the character follows the path.
Answer by MrCam · Feb 19, 2019 at 02:32 PM
Complementing TakuanDaikon's answer, at line 20 you can just: linerenderer.setPositions(path.corners)
Answer by mukundrungta · Jun 26, 2016 at 06:00 AM
This is not working... can you explain me why ? Where to call this function from because adding this function doesnot show me any path being drawn? Please reply asap... this is very urgently required.
You need to SetDestination for your Nav$$anonymous$$eshAgent. Using the example above as reference, you need to add this:
nav.SetDestination(transform.position);
This code seems to be a bit old, compared to the current C# version.
This is why it is not working.
You can try to change a few syntax to get it running again, I guess.
Your answer
Follow this Question
Related Questions
NavMeshAgent collision not working with player 2 Answers
How to queue NavMeshAgents on entering a tile in Unity (based on path distance to tile)? 0 Answers
NavMesh Agent rotation problem 3 Answers
Making NavMesh areas? 0 Answers
How to prevent NavMeshAgents from colliding when warped to same position? 0 Answers