Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by NumberOne1 · May 16 at 09:51 AM · loopnavigationvector3.lerp

Getting random values out of while loop for unknown reason

Im trying to have a path only have a max distance regardless of where the initial destination is. to do this I'm attempting to use Vector3.lerp in a while loop. it appears to work sometimes but other times is giving seemingly random values and I cant figure out why.

Code:

{

 private NavMeshAgent myAgent;
 private NavMeshPath path;

 [SerializeField] LineRenderer line;


 float pathDistance;

 Vector3 FinalPos;
 Vector3 p2;
 Vector3 p1;

 [SerializeField] float moveSpeed;



 // Start is called before the first frame update
 void Start()
 {
     myAgent = GetComponent<NavMeshAgent>();
     
 }

 // Update is called once per frame
 void Update()
 {
     Movement();



 }

 void Movement()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;

         if (Physics.Raycast(myRay, out hit, 100f, whatCanClick))
         {
             float a = 0.9f;
             myAgent.isStopped = true;
             myAgent.SetDestination(hit.point);
             pathDistance = CalculatePathLenth();

             while(pathDistance > moveSpeed)
             {
                 
                 FinalPos = Vector3.Lerp(p1, p2, a);
                 a -= 0.001f;
                 myAgent.SetDestination(FinalPos);
                 pathDistance = CalculatePathLenth();

                 if (a < 0.001f) break;

             }
             print(pathDistance);
             
             a = 0.999f;




             line.positionCount = myAgent.path.corners.Length;
             line.SetPositions(myAgent.path.corners);
             line.enabled = true;
             
             

         }
     }


 }

 float CalculatePathLenth()
 {
     float distance = 0f;

     for (int i = 0; i < myAgent.path.corners.Length - 1; i++)
     {
          p1 = myAgent.path.corners[i];
          p2 = myAgent.path.corners[i + 1];

         distance += Vector3.Distance(p1, p2);

     }
     return (distance);


 }

}

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 rh_galaxy · May 16 at 01:05 PM

If myAgent.path.corners.Length is less than 2, p1 and p2 would be undefined. I'm not sure that is the problem, but maybe you could input the max distance in CalculatePathLenth() and skip your while loop and let the function set FinalPos.

 float CalculatePathLenth(float max) //sets FinalPos
 {
     float distance = 0f;
     //FinalPos = startpos; //maybe you need this?
     for (int i = 0; i < myAgent.path.corners.Length - 1; i++)
     {
         p1 = myAgent.path.corners[i];
         p2 = myAgent.path.corners[i + 1];
         float segmentlength = Vector3.Distance(p1, p2);
         if(distance+segmentlength>max)
         {
             float into = ((max-distance)/segmentlength); //0..1 into the current seg
             FinalPos = Vector3.Lerp(p1, p2, into);
             return max;
         }
         distance += segmentlength;
         //FinalPos = p2; //maybe you need this?
     }
     return (distance);
 }
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 NumberOne1 · May 16 at 09:44 PM 0
Share

That solved the issue and was much more straightforward than a while loop. Thank you!

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

183 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 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 change a variable used in a loop for the outside ? 1 Answer

Looped Animation 0 Answers

How do I loop music in C# 1 Answer

Stucked in a infinite loop and don't know why 2 Answers

running a while loop until coroutine finishes 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