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 /
This question was closed Jul 19, 2015 at 03:34 PM by KazeEnji for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by KazeEnji · Jul 19, 2015 at 02:05 AM · unity 5programmingforeachwaypoint

How to move a player automatically through a List of Waypoints?

So I have a script that calculates a path through a series of branching waypoints and returns a list of waypoints to follow when the player clicks on an acceptable destination. So the player has a List of GameObjects which contains the path through the waypoints in order. Now how do I make them move?

I've tried:

 void MovePlayer1()
 {
     foreach(GameObject _item in pathList)
     {
         if(clearToMove == true)
         {
             transform.position = Vector3.MoveTowards(transform.position, _item.position, step * Time.deltaTime;
         }
     }
 }

I've tried tying in an IEnumerator to wait for a second before checking again. I think it's just executing too fast and nothing is happening.

I was thinking about trying to do this in the Update() method but since the movement needs to wait for me to click on a waypoint, it need some way to prevent it from executing in Update() and I need to make sure that it waits to move to the next point in the list until it reaches its current destination.

Anyone have any ideas? I'm open to other movement solutions not along these lines too. If you pretty much just start your suggestions at the point of knowing you have a list to follow, that's what I'm looking for.

Thanks! -Kaze-

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

Answer by zach-r-d · Jul 19, 2015 at 08:01 AM

It'd probably be easiest to just do the whole thing in a coroutine/IEnumerator:

 void MovePlayer1() {
     StartCoroutine(MovePlayer1Coroutine());
 }
 
 IEnumerator MovePlayer1Coroutine() {
     foreach(GameObject _item in pathList) {
          Vector3 itemPos = _item.transform.position;

          while (Vector3.Distance(transform.position, itemPos) > .0001) {
              transform.position = Vector3.MoveTowards(transform.position, itemPos, step * Time.deltaTime);
              yield return null;
          }
      }
 }
Comment
Add comment · Show 2 · 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 KazeEnji · Jul 19, 2015 at 03:34 PM 1
Share

Yaaay! It worked! Thank you so much.

There was one error in your script, when you're doing the while loop, it needed to be a ">" not a "<".

But other than that, it worked perfectly.

avatar image zach-r-d · Jul 19, 2015 at 09:26 PM 0
Share

Oh, nice catch! I'll edit the answer. That's what I get for writing code without testing I guess.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Multiple Cars not working 1 Answer

How Can I Display UnityEvent in Custom Inspector When it is a Property of a Properties Property? (C#) 1 Answer

Problem with "Using Unity Standard Assets" in script C# 1 Answer

Need help in Stone throwing mechanic with aim in unity 3d C# 0 Answers

Hint Joint like a rope have weird behavior 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