Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by goldfido · Dec 10, 2012 at 04:04 PM · navmeshnavigationlinedrawingagent

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
8

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 ] );
     }
 
 }
 
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image exorakhilas · Oct 21, 2015 at 09:44 AM 1
Share

Thank you for sharing!

avatar image chainalonez · Mar 23, 2016 at 04:07 PM 1
Share

it's nice sample, thanks a lot :)

avatar image Fredex8 chainalonez · Mar 23, 2016 at 04:45 PM 4
Share

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);
             }
avatar image TakuanDaikon Fredex8 · Mar 23, 2016 at 06:24 PM 1
Share

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 :)

avatar image ranybechara · Jan 25, 2017 at 04:06 PM 0
Share

This is great thanks ! but It only works when I click on the agent I have the script on in the hierarchy tab.

avatar image TakuanDaikon ranybechara · Jan 25, 2017 at 04:53 PM 1
Share

Right, because it's OnDrawGizmosSelected(). You can rename it to OnDrawGizmos() if you want it displayed all the time ins$$anonymous$$d.

avatar image help_seeker · Mar 04, 2017 at 01:32 PM 0
Share

hey man can i restrict two navmesh agents to follow two specific paths? if possible please share a piece of code then

avatar image DarshanP · Aug 04, 2017 at 10:54 AM 0
Share

How to leave the line permanently and not make it disappear as the character follows the path.

avatar image
1

Answer by MrCam · Feb 19, 2019 at 02:32 PM

Complementing TakuanDaikon's answer, at line 20 you can just: linerenderer.setPositions(path.corners)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kyrnal · Aug 02, 2016 at 11:18 PM 0
Share

You need to SetDestination for your Nav$$anonymous$$eshAgent. Using the example above as reference, you need to add this:

nav.SetDestination(transform.position);

avatar image Kr8vKhan · Aug 02, 2017 at 11:00 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges