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 /
This question was closed Feb 22, 2017 at 03:40 PM by LandonC for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by LandonC · Oct 28, 2014 at 04:28 AM · javascriptnavmeshpathfindingyieldwait

[Javascript] Creating waiting points for random movement with NavMesh Script

Hi, Im facing a problem creating waiting points for a NavMesh Script I got on Unity Answers, its pretty much a script just to create random points for the character with NavMeshAgent to move on a NavMesh plane, but problem is that I want the character to pause on every point reached and wait for several seconds before finding a new path and I found out that it is not possible to have a yield on function Update. I tried to add an Idle function after end of path is reached but it doesn't work. I'm new with unity scripting, so I am basically just messing around with the scripts, any ideas on how to make it possible? a solution is much appreciated!

 #pragma strict
 import System.Collections.Generic;
  
 var navMeshScript : navMesh;
 var path : List.<Vector3> = null;
 var targetNodeIndex : int = 0;
 var targetNode : Vector3;
 
 var idleTime = 2;
 
  
 function Start () {
     //Find the navmesh script in the scene
     navMeshScript = GameObject.FindObjectOfType(navMesh).GetComponent("navMesh");
 }
  
 function Idle ()
 {    
 
 //    print ("idle");
     yield WaitForSeconds(idleTime);
     
     while (true)
     {
         yield WaitForSeconds(5);
         
         
     }
 }
     
    
 
 
 function Update () {
 
 
     //If the path is null
     if (path == null) {
         //Find a new random path
         Idle();
         path = navMeshScript.findRandomPath(transform.position);
         targetNodeIndex = 5;
     }
   
     //The end of the path hasn't been reached
     if (targetNodeIndex < path.Count) {
         targetNode = path[targetNodeIndex];
        
         //If the character is close to the target node
         if (Vector3.Distance(transform.position, targetNode) < distanceToNode) {
             //Target the next node
             targetNodeIndex += 1;
         }
     }
     
     //The end of the path has been reached
     else {
         path = null;
         
         
     }
     
 
     transform.position += moveSpeed*(targetNode-transform.position).normalized*Time.deltaTime;
     transform.LookAt(targetNode);
      Idle();
     
     }




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

  • Sort: 
avatar image
0

Answer by protopop · Feb 05, 2016 at 04:42 PM

put the yield inside a separate function and call that function from your update loop

 function newPatrol(){
     yield WaitForSeconds(5.0);
         findNewPathOrWhatever();
 }    

@LandonC

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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problem with yield waitForSeconds in for loop 0 Answers

NavMesh area, walkable but not accessible. 2 Answers

NavMeshAgent and NavMeshObstacle together, unit jumping a small distance on toggle 2 Answers

Is there a limit on how far a navmesh agent can go? 0 Answers

Navmesh on all sides of an object? 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