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
1
Question by MakerDavid · Apr 12, 2016 at 07:51 PM · navmeshagentpathfindinglinerenderer

Linerenderer Inconsistency

Hi, I am trying to draw a line between my nav mesh agent and it's destination.

https://gyazo.com/0ac93ab1199111d878ee1c234478650c

This is how it works so far. It works, but sometimes the line doesn't appear, or it appears on the next click (one click to late). It usually doesn't appear if I click too far away.

I want to click somewhere and then show the path already, and it does this sometimes. I am not sure what I have to do to increase the performance of this linerenderer.

Linerenderer script.

 using UnityEngine;
 using System.Collections;
 
 public class PathLine : MonoBehaviour {
     LineRenderer Line;
     NavMeshPath Path;
     public Material mat; 
 
     void Start () {
         Line = GetComponent<LineRenderer>();
        
     }
     
     public void Pathlinetex(NavMeshPath Path) {
         Line.material = mat; 
             //new Material(Shader.Find("Sprites/Default")) { color = Color.red };
             Line.SetWidth(0.20f, 0.20f);
             //Line.SetColors(Color.red, Color.red);
             Line.SetVertexCount(Path.corners.Length);
 
         for (int i = 0; i < Path.corners.Length; i++)
             {
                 Line.SetPosition(i, Path.corners[i]);
             }
 
         }
     }

ClickToMove Script.

 using UnityEngine;
 using System.Collections;
 
 public class ClickMove : MonoBehaviour {
 
     NavMeshAgent navAgent;
     NavMeshPath ppath;
     static Animator aanimation; 
     
 
     void Start ()
     {
         navAgent = GetComponent<NavMeshAgent>();
         aanimation = GetComponent<Animator>(); 
     }
 
     void Update()
     {
         Animating(); 
         RaycastHit hit; //Hold information where raycast hits terrain
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // The ray goes from camera to mouse
         if (Input.GetMouseButtonDown(0))
         {
             if (Physics.Raycast(ray, out hit, 100))
             {
                 navAgent.SetDestination(hit.point);
                 ppath = navAgent.path;
                 GetComponent<PathLine>().Pathlinetex(ppath); 
             }
         }
         
     }
 
     void Animating()
     {
         if (navAgent.velocity.magnitude > 1f)
         {
             aanimation.SetBool("isSneaking", true);
         }
         else {
             aanimation.SetBool("isSneaking", false); 
 
         }
 
     }
 }





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
Best Answer

Answer by Bunny83 · Apr 13, 2016 at 12:05 AM

Just read the docs carefully:

Note that the path may not become available until after a few frames later. While the path is being computed, pathPending will be true. If a valid path becomes available then the agent will resume movement.

The pathfinding is done asynchonously in the background so you don't get live feedback. If you need to know the path immediately you might want to look at NavMeshAgent.CalculatePath. However it's probably better to check NavMeshAgent.pathPending in Update or a coroutine and do your linerenderer settings when the path has been calculated. A Coroutine will most likely be the best solution.

 IEnumerator SetTarget(Vector3 pos)
 {
     navAgent.SetDestination(hit.point);
     while (navAgent.pathPending)
         yield return null;
     ppath = navAgent.path;
     GetComponent<PathLine>().Pathlinetex(ppath); 
 }

And in your click method simply do:

          if (Physics.Raycast(ray, out hit, 100))
          {
              StartCoroutine(SetTarget(hit.point));
          }


Comment
Add comment · Show 1 · 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 MakerDavid · Apr 13, 2016 at 11:26 AM 0
Share

Thank you! I love you :) It works perfectly :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Navmesh with destructible obstacles 1 Answer

Custom Ai Movement using Unity's pathfinding 1 Answer

NavMeshAgent Finding Alternative Route 1 Answer

Connecting two navmeshes without "Speedboost" 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