Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by TPorteau · Jul 09, 2019 at 10:18 AM · navmeshnavmeshagentpathfinding

How to display the trajectory of a nav mesh agent in runtime ?

Hi, I'm making a tactical rpg based on Divinity Orginal Sin 2 and to move the character I use Unity's pathfinding, but I'd like the player to see which path the character will take before moving. I attach an image of what it looks like in Civ 5 (It's not a tactical rpg but it works the same way). I have already tried to do it with the "render line" component but it was not working well. Thank you in advance for your help.


alt text

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by I_Am_Err00r · Jul 09, 2019 at 01:41 PM

I know you said you already tried the render line component, but that really seems to be the best way and I would try that a little more before giving up; I found a link that looks very straightforward for you to create a script with and attach to whatever you are trying to draw the path for:

https://gamedev.stackexchange.com/questions/67839/is-there-a-way-to-display-navmesh-agent-path-in-unity

I have edited the above code, add this code to a new script you would create for your object and you can even plug in each one through the inspector:

 public class Example: MonoBehaviour
 {
     public LineRenderer line; //to hold the line Renderer
     public Transform target; //to hold the transform of the target
     public NavMeshAgent agent; //to hold the agent of this gameObject
 
     public void GetPath()
     {
         line.SetPosition(0, transform.position); //set the line's origin
 
         agent.SetDestination(target.position); //create the path
         yield WaitForEndOfFrame(); //wait for the path to generate
 
         DrawPath(agent.path);
 
         agent.Stop();//add this if you don't want to move the agent
     }
 
     public void DrawPath(NavMeshPath path)
     {
         if(path.corners.Length < 2) //if the path has 1 or no corners, there is no need
             return;
 
         line.SetVertexCount(path.corners.Length); //set the array of positions to the amount of corners
 
         for(int 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
         }
 }

I hate to say it but if that doesn't make sense then there wouldn't be a more straightforward way to do what you are trying to do and that is probably the easiest solution available to you and literally made to do what you are asking.

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 Hellium · Jul 09, 2019 at 01:48 PM 0
Share

I would use CalculatePath ins$$anonymous$$d of setting the destination and then, stopping the agent.

https://docs.unity3d.com/ScriptReference/AI.Nav$$anonymous$$eshAgent.CalculatePath.html


I would also check whether the computed path is computed before trying to draw it.

https://docs.unity3d.com/ScriptReference/AI.Nav$$anonymous$$eshAgent-pathPending.html

avatar image TPorteau · Jul 09, 2019 at 03:06 PM 0
Share

Thank you for your answer but I have an error and several warnings on the code you sent me in particular the "yield WaitForEndOfFrame();"... In the idea the draw line component works it's just that I don't find it very aesthetic and half the line is cut in the ground (because of the orientation of my camera)... Can't we ins$$anonymous$$d create meshes all along the path? I have seen on the net that people manage to do it with paths they have traced in editor (https://forum.unity.com/threads/placing-points-along-a-circular-path-relative-to-a-rotation.427139/), we can't readapt this method to the path of navmesh in real time?

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

117 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 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 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 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 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 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

Navmesh with destructible obstacles 1 Answer

How to sample a point on a navMesh Agent path 0 Answers

navmesh.sampleposition works strangely, what is going on here? 0 Answers

CalculatePath often returns wrong results 0 Answers

How to build a custom shape NavMesh Area 1 Answer


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