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
0
Question by IvyMoon · Jun 14, 2018 at 06:33 AM · arraysloopwaypointwaypoint system

How do I make a waypoint loop function using array? - when it gets to a waypoint, then move to next waypoint

I've written this code that works, but i must keep repeating it over and over in script. I feel there must be a way to do this with an array but I'm having a hard time figuring it out.

     if (state == 0)
         {
             float speed = movementSpeed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, waypoint.transform.position, speed);
             if (Vector3.Distance(transform.position, waypoint.transform.position) <= 0)
             {
                 state = 1;
             }
         }
         if (state == 1)
         {
             float speed = movementSpeed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, waypoint1.transform.position, speed);
             if (Vector3.Distance(transform.position, waypoint1.transform.position) <= 0)
             {
                 state = 2;
             }
         }
         if (state == 2)
         {

etc.... (copy paste for eternity)

Here is where i am stuck(my array attempt):

         for (int i = 1; i < waypoints.Length; i++)
         {        
            // print(Vector3.Distance(transform.position, waypoints[i].transform.position));
             float speed = movementSpeed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, waypoints[i].transform.position, speed);
             if (Vector3.Distance(transform.position, waypoints[i].transform.position) < 1)
             {
               //  break;
             }

The problem i have when this runs is that the object just moves to the 1st waypoint and becomes stuck there...

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
0

Answer by ShadyProductions · Jun 14, 2018 at 07:05 AM

Well you have to repeat for aslong as the object has not arrived to the waypoint. And you should probably start your array at 0 because arrays are 0 based.

  for (int i = 0; i < waypoints.Length; i++)
  {        
     // print(Vector3.Distance(transform.position, waypoints[i].transform.position));
      float speed = movementSpeed * Time.deltaTime;
      while (Vector3.Distance(transform.position, waypoints[i].transform.position) > 1)
      {
            transform.position = Vector3.MoveTowards(transform.position, 
            waypoints[i].transform.position, speed);
      }
 }
Comment
Add comment · Show 4 · 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 Zodiarc · Jun 14, 2018 at 09:58 AM 0
Share

Also general hint: Ins$$anonymous$$d of using arbitrary numbers for states, use enums with meaningful names. I guarantee in 2 weeks you won't know what each number represents.

avatar image ShadyProductions Zodiarc · Jun 14, 2018 at 10:17 AM 0
Share

Well you don't need those states anymore anyway with this solution..

avatar image Zodiarc ShadyProductions · Jun 14, 2018 at 01:57 PM 0
Share

But it's still good advice :P

avatar image IvyMoon · Jun 14, 2018 at 03:57 PM 0
Share

Actually state numbers work fine for me, but thanks for your thoughts...I leave comments where i need more info

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

86 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

Related Questions

Using arrays and for loop with GetComponent error 1 Answer

How will i make my objects/model(say a car) face the way points and travel towards it,instead of blindly following it? 2 Answers

How to make a gameObject follow a player 1 Answer

Assign Default For Large Array 1 Answer

Making users able to make custom tracks to follow by the character in the game 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