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 /
avatar image
0
Question by trungleau17 · Jul 22, 2020 at 06:16 PM · vector3for-loopforeach

For loop update variables value before functions complete

Hi everyone, I'm currently stuck in a situation like this: I have a Script with 3 functions to move my Object in the following order (rotate, move up, and move forward) follow a list of Vector3 coordinates. Each function uses a Vector3 point to work. My goal is to move my object to each target point of the list using the action set above (reset the process after the object has reached a target point). When I test with each individual point, everything works normally However, when I put the list of target points in a for loop to access each elements, the objects immediately move to the last position instead of going through every point of the list Is there a solution for this problem, I've been stuck with this problem for days :(

 public void ObjectPathFollowing(List<Vector3> v)
 {
     for(i = 0; i < v.Count; i++)
     {
         if(state == 0)
         {
             Rotation(v[i]);
         }
         if(state == 1)
         {
             MovingUp(v[i]);
         }
         if(state == 2)
         {
             MovingFoward(v[i]);
         }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by smark12007 · Jul 22, 2020 at 06:30 PM

Maybe you need to wait for seconds?

 public void ObjectPathFollowing(List<Vector3> v)
 {
     StartCoroutine(ObjectPathFollowingEnumerator(v));
 }
 System.Collections.IEnumerator ObjectPathFollowingEnumerator(List<Vector3> v)
 {
     for (i = 0; i < v.Count; i++)
     {
         if (state == 0)
         {
             Rotation(v[i]);
         }
         if (state == 1)
         {
             MovingUp(v[i]);
         }
         if (state == 2)
         {
             MovingFoward(v[i]);
         }
         yield return new WaitForSeconds(0.2f);
     }
 }
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 trungleau17 · Jul 23, 2020 at 03:39 AM 0
Share

Hi smark12007, Thank you for your solution, it really helps me a lot Following your solution, I've adjusted my code and the object had started to move However, it only moved to the first point of the list and stop Do you know how to fix that?

avatar image smark12007 trungleau17 · Jul 23, 2020 at 10:52 AM 0
Share

You might need to show more details of your scripts. Like: Where the program calls ObjectPathFollowing. How state works. Why i is not declared inside the function.

avatar image
0

Answer by Weaver13 · Jul 23, 2020 at 04:06 AM

Classes make everything so much nicer. Untested but more or less what I always do.

 public class Navigation{
     Transform player;
     vector[] points;
     int index = 0;
     
     public void update(){
         float distance = Vector3.Distance(player.position, points[index];
         if (distance < 0.1f) index += 1;
         if (index >= points.Length) index = 0; //or whatever, maybe choose new array to work on
         Vector3 dirToMove = points[index] - player.position;
         player.MoveForwardOrUpOrWhatever(dirToMove.normalized * speed);
     }
     public Navigation(MyMonoBehaviourPlayer, params Vector3[] stuffids){
         player = MyMonoBehaviourPlayer.transform;
         points = stuffids;
     }
 }
 
 public class MyMonoBehaviourPlayer : MonoBehaviour{
     public Navigation nav;
     puvlic void Start(){
         nav = new Navigation(this);
     }
     public void FixedUpdate(){
         if (nav != null) nav.update();
     }
 }


If you are moving the exact amount to the position every frame, then simply add 1 to index every update instead of checking for distance to player.

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

163 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

Related Questions

Why letters appear in reversed order ? ,Letters appear in reversed order 2 Answers

Copy values between two classes in two lists. 1 Answer

Instantiate for loop skips some objects 1 Answer

ScrollableList best way to implement 0 Answers

my wave spawner not working properly 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