Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 pineApples · Jul 13, 2015 at 01:22 AM · coroutinewaypointswaypoint system

How to repeat a coroutine with an event?

Firstly, I am unsure whether "repeat" is the right terminology (feel free to correct me on that). Secondly, I am making a waypoint-based navigation for NavMesh agent to follow. The problem is that I am using a timer (i.e WaitForSeconds) to assign the waypoint to the agent (if I don't do this the loop will run just 1 frame and the agent just moves to the last waypoint).

Here is the pseudocode of what I was want to do:

 if (agent reaches a waypoint)
 {
        agent.setDestination(nextWaypoint);
 }

But I do not know how to do this.

Here is what I have so far:

 IEnumerator wayPointPicker()
 {
     for (int i = 0; i < waypoint.wayPointPositionList.Length; i++)
     {
             opponentAgent.SetDestination(waypoint.wayPointPositionList[i]);
             
             yield return new WaitForSeconds(timer);
     }
 }

If you need more information to go on for I will try my best to supply. I will be very grateful if you can type a code snippet along with your explanation - preferably in C# but I can make do with JavaScript.

Also if you have another method on tackling waypoints - feel free to share! I find techniques on solving the same problems fascinating!

Thanks for taking the time to read this far!

Comment
Add comment · Show 3
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 maccabbe · Jul 13, 2015 at 03:01 AM 0
Share
  IEnumerator wayPointPicker()
  {
      for (int i = 0; i < waypoint.wayPointPositionList.Length; i++)
      {
              opponentAgent.SetDestination(waypoint.wayPointPositionList[i]);

              // wait until waypoint is reached
              while(agent not at waypoint){
                  // if not at waypoint then wait till next frame
                  yield return null; 
                  // can also use WaitForSeconds ins$$anonymous$$d of a frame
              }
      }
  }

avatar image pineApples · Jul 13, 2015 at 11:03 AM 0
Share

It does not seem to work. The agent only moves to the first destination and stops there. It happens to both "yield return null" or "yield return new WaitForSeconds".

Here's how I interpreted your suggestion into my code:

     IEnumerator wayPointPicker()
     {    
         for (int i = 0; i < waypoint.wayPointPositionList.Length; i++)
         {
             opponentAgent.SetDestination(waypoint.wayPointPositionList[i]);
             while (transform.position != waypoint.wayPointPositionList[i])
             {
 //                yield return new WaitForSeconds(timer);
                 yield return null;
             }
         }
avatar image meat5000 ♦ · Jul 14, 2015 at 10:05 PM 0
Share

Your title is misleading.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ibzy · Jul 13, 2015 at 12:35 PM

I'm not entirely sure if the destination gets cleared when an agent reaches its destination, but if it does, a check of opponentAgent.HasDestination() could be used to check if they've made it to the waypoint.

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 pineApples · Jul 14, 2015 at 09:48 PM 0
Share

According to Unity scripting API, there is no ".HasDestination()" but a ".hasPath()" (if that is what you have meant?)... and where would I add this check in the coroutine?

avatar image
0

Answer by meat5000 · Jul 14, 2015 at 10:02 PM

Im not sure if this is what you are after, but I use the child's/monkey's approach, it seems (For a predefined path).

Here's some snippets.

The first is the OnDrag routine which records my swipe across a RenderTexture from an overhead camera.

The Second is the simple timed stepping through the elements of the created List containing the points, and a Lerp to project my spaceship through the path.

Its pretty crude but you should be able to see how it works.


     function OnDrag(data : PointerEventData)
     {
         if(enlarged)
         {
             didDrag = true;
             var hitR : RaycastHit;
             var maskL : LayerMask = 1 << 14;
             var rayTouch : Ray = myCam.ScreenPointToRay(data.position);
             if(Physics.Raycast(rayTouch, hitR, Mathf.Infinity, maskL))
             {
                 this.playerPath.Add(Vector3(hitR.point.x, 10, hitR.point.z));
             }
         }
     }

     function FixedUpdate ()
     {
         nextPos = lastPos + 1;
         if(getPath)
         {
             GetPath(); //Returns playerPath List
             getPath = false;
             followPath = true;
         }
         if(followPath && nextPos < myPath.Count)
         {
             timer += Time.deltaTime;
             if(timer > timerMax)
             {
                 lastPos += 1;
                 timer = 0.0;
                 if(!firstPosHit) firstPosHit = true;
             }
             var t : float = timer / timerMax;
             var temp1 : Vector3 = Vector3(myPath[lastPos].x, 10, myPath[lastPos].z);
             var temp2 : Vector3 = Vector3(myPath[nextPos].x, 10, myPath[nextPos].z);
             
             if(!firstPosHit)
             {
                 transform.position = Vector3.Lerp(transform.position, temp1, t);
             }
             else transform.position = Vector3.Lerp(temp1, temp2, t);
         }
         else followPath = false;
         //Debug.Log(temp1+"    "+temp2);
     }
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

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

Getting script to update list of transforms when destination reached or the transform's game object is inactive 0 Answers

PacMan IA of ghost using nodes/waypoints 0 Answers

Kind of Lag in movement 2 Answers

How to move the virtual camera at specific waypoints (generated from an optimal path finding algorithm for a building model )around the same 3D model of building? 1 Answer

Make player follow a waypoint path but still be able to control player movement 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