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 s9933778 · Sep 11, 2019 at 02:53 PM · scripting problempathfindinglinerendererpathagent

Problem with calculated path of a NavMeshAgent

Hey guys,
I'm currently working on a script to draw the calculated path of an NavMeshAgent. Therefore I use a LineRenderer. The corners of the calculated path are taken to set the points of the LineRenderer.

The problem: The agent always reaches its destination, but depending on the destination the path is not always drawn correctly. In my case the agent should move from one room in a building to another (I took a sphere as destination). In the first situation the agent and the destination are on the same storey and the path is shown correctly. In the next situation I set the destination one storey below and the path is drawn just at the start. Nevertheless the agent moves to the sphere. See the pictures below. alt text alt text While debugging, I could see that the path (path.corners.Length) only has two corners when shown incorrectly (and more when shown correctly). This would explain the short LineRenderer in the second picture, because the LineRenderer is generated out of the corners of the path, so it should always be identical. But why does the agent then reach its target? This would mean the stored path (agent.path) differs from the one the agent actually takes.

     public Transform goal;
     public LineRenderer line;
     public NavMeshAgent agent;
     public GameObject LR;
 
     void Start()
     {
         Debug.Log("Start!");
 
         LR = this.gameObject.transform.GetChild(0).gameObject;
         line = LR.AddComponent<LineRenderer>();
         line.SetWidth(0.3f, 0.3f);
         agent = GetComponent<NavMeshAgent>();
         Debug.Log("Agent erstellt");
         //Coroutine = GetPath();
         StartCoroutine(GetPath());
         //GetPath();
 
     }
 
     IEnumerator GetPath()
     {
         Debug.Log("GetPath gestartet!");
         //yield return new WaitForSecondsRealtime(5);
         Debug.Log(transform.position);
         line.positionCount = 1;
         line.SetPosition(0, transform.position);  //set the line's origin
 
         agent.SetDestination(goal.position);
         //agent.CalculatePath(goal.position);
         Debug.Log("Dem Agenten ein Ziel zugewiesen");
         Debug.Log("Anzahl der Linienpunkte (Pfad): " + agent.path.corners.Length);
         yield return new WaitForEndOfFrame(); //wait for the path to generate
         Debug.Log("Nach EndOfFrame: Anzahl der Linienpunkte (Pfad): " + agent.path.corners.Length);
         DrawPath(agent.path);
 
         Debug.Log("Ende von GetPath!");
         //throw new NotImplementedException();
     }
 
     void Update()
     {
 
         //StartCoroutine(GetPath());
     }
     private void OnDrawGizmos()
     {
         // Draw a yellow sphere at the transform's position
         //Gizmos.color = Color.yellow;
         //Gizmos.DrawSphere(transform.position, 1);
     }
     void DrawPath(NavMeshPath path)
     {
         Debug.Log("DrawPath gestartet!");
         if (path.corners.Length < 2) //if the path has 1 or no corners, there is no need
         {
             Debug.Log("DrawPath.Return!");
             return;
         }
         line.positionCount = path.corners.Length; //set the array of positions to the amount of corners
         Debug.Log("Anzahl der Linienpunkte (Pfad): " + path.corners.Length);
         Debug.Log("Anzahl der Linienpunkte (Line): " + line.positionCount);
         for (var 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
             Debug.Log(i + ", " + line.GetPosition(i));
 
         }
         Debug.Log("Ende von DrawPath!");
     }

path-shown-correctly.png (70.2 kB)
path-not-shown-correctly.png (65.3 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

112 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

Related Questions

How to minimize directional changes of A* autopathing movement? 0 Answers

Is it possible to store NavMeshAgents paths and assign them to other NavMeshAgents later? 1 Answer

NavMesh Update Path 0 Answers

Smooth out a line renderer 1 Answer

I want to draw a line(curved) as finger moves on screen after that move a instantited gameobject/rigidbody following that path. 2D 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