Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Mickross · May 25, 2019 at 03:30 PM · vector3coroutinehexagon

Move along several Vector3 in a coroutine

Hello,

I have an hexagonal board, where I can click and draw a path with the mouse to move my character along this path: alt text The Vector3 position of each cell is store in a Vector3 array and my problem is to be able to move smoothly my character along each cell. For that I try with a coroutine:

 void Update() {
     if (...) {
         StartCoroutine(Move_Routine(this.transform, listPathTransform));
     }
 }

 private IEnumerator Move_Routine(Transform transform, List<Vector3> vectors) {
     for(int i = 0; i < vectors.Count - 1; i++) {
         float t = 0f;
 
         Vector3 a = vectors[i];
         Vector3 b = vectors[i+1];
 
         while (t < 1f) {
             t += Time.deltaTime;
             transform.position = Vector3.Lerp(a, b, Mathf.SmoothStep(0, 1, t));
             yield return null;
         }
             
     }
 }

I am not very confortable with coroutine yet, and I don't know if it is a good idea to use it for this problem. The result of my code is that the character will move smoothly from its position to the first selected cell but will not continue (like the for loop never increase i). Do you have any idea why is it like this or any suggestion about how to solve this problem (coroutine or not)? Thank you!

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

Answer by Bunny83 · May 25, 2019 at 05:48 PM

I don't see an issue with your code. The only thing that could be problematic is that you use a List which could be changed outisde the coroutine while the coroutine is running. Are you sure that you don't clear the list after you started the coroutine? If you want to clear it you have to provide a copy of the list.

Personally I would do the loop like this:

 if (vectors.Count == 0)
     yield break;
 Vector3 start = vectors[0];
 for(int i = 1; i < vectors.Count; i++)
 {
     Vector3 end = vectors[i];
     float t = 0f;
     while (t < 1f)
     {
         t += Time.deltaTime;
         transform.position = Vector3.Lerp(start, end, Mathf.SmoothStep(0, 1, t));
         yield return null;
     }
     start = end;
 }

Though yours should work equally well. If you're not sure if the list might be changed during the coroutine you can create an array at the beginning

 private IEnumerator Move_Routine(Transform transform, List<Vector3> vectors)
 {
     Vector3 points = vectors.ToArray();
     for(int i = 1; i < points.Length; i++)
     {
         // [ ... ]

This ensures no trouble with any changes to the list.

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
avatar image
0

Answer by Mickross · May 26, 2019 at 09:25 PM

Hey @Bunny83, thank you very much, it works perfectly. My problem was that I clear my list as you expected. My knowledge for coroutine wasn't high enough to understand all the behavior of it. Thank you again!

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

139 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

Related Questions

How to calculate the rotation of a hexagon based on its six vertices position 2 Answers

Activating 3 rotations based on multiple presses on one key 2 Answers

Code working on wrong axis. 1 Answer

Method for Updating Physics Vectors 0 Answers

Moving the player toward a direction 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