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
-1
Question by AhmedGalal1987 · Jul 17, 2017 at 06:21 AM · unity 5positioncoroutinemovetowards

How to move object repeatedly with Vector3.MoveTowards

So now i have a boat out of the camera and i want to implement this scenario :

1- When start game, waiting for 5 seconds then move the boat from position A to position B. 2- When boat reach position B the boat will get back to position A instantly.
3- Then wait for 5 seconds again and repeat 1 and 2 steps

And so on ..

alt text



This is my code but the problem that when start the game, the boat starting move without waiting seconds and once it reach to position B it get moved again and also increase its speed.

 // Use this for initialization
     void Start () {
         startPos = transform.position;
         moveBoats = MoveBoats ();
 
     }
     
     // Update is called once per frame
     void Update () {
         StartCoroutine (moveBoats);
         if (transform.position.x == boatEndPoint.transform.position.x) {
             transform.position = startPos;
         }
     }
 
     IEnumerator MoveBoats ()
     {
         while (true) {
             yield return new WaitForSeconds (5.0f);
             transform.position = Vector3.MoveTowards (transform.position, boatEndPoint.transform.position, speed * Time.deltaTime);
         }
     }

How can i fix that ??

captureboat.png (149.9 kB)
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 SohailBukhari · Jul 17, 2017 at 07:01 AM

in the Start method just pass the points a and b and and time to move.Lets suppose you want to move from point a to point b within 5 seconds then yield return StartCoroutine(MoveObject(transform, pointA, PointB, 5.0f));

 public Vector3 PointB;
 
     IEnumerator Start()
     {
         var pointA = transform.position;
         while (true)
         {
             yield return StartCoroutine(MoveObject(transform, pointA, PointB, 3.0f));
             yield return StartCoroutine(MoveObject(transform, PointB, pointA, 3.0f));
         }
     }
 
     IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
     {
         yield return new WaitForSeconds(5f);// first yield return after five seconds as you want
         var i = 0.0f;
         var rate = 1.0f / time;
         while (i < 1.0f)
         {
             i += Time.deltaTime * rate;
             thisTransform.position = Vector3.Lerp(startPos, endPos, i);
             yield return null;
         }
     }

Arrange time when your boat come and at which speed.

you can also use MoveTowards Scripting API to move.

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

Index out of range in while statement but I can't figure out how. 1 Answer

Copy position from one object to another on one axis 0 Answers

reset my Y axis to 0 every time leaving 0 1 Answer

Camera rotation around player while following. 6 Answers

Coroutine executes unrelated object's code 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