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 isse26 · Jan 08, 2014 at 06:19 PM · ienumerator

Why Dose not this work (IEnumerator)

Hi. I'm having trouble with my script. I am trying to make my object move from A to B in x amount (this case 4) seconds; But when I run my script the objects just flies away and the teleports to point B;

Please help

         if (Input.GetKey(KeyCode.H))
         {
             StartCoroutine(Move(transform.position, HomePos.transform.position, 3.0f, this.gameObject.transform));
         }
 
 
 IEnumerator Move(Vector3 A, Vector3 B, float time, Transform target)
         {
         float speed = Vector3.Distance(A,B) / time; 
 
         Vector3 direction = (B - A).normalized;
 
         target.position = A;
 
         float elapsed = 0.0f;
 
         while (elapsed < time) 
         {
             elapsed += Time.deltaTime;
             target.Translate(direction * speed * Time.deltaTime);
             yield return true;
         }
         transform.position = B;
     }
Comment
Add comment · Show 1
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 robertbu · Jan 08, 2014 at 06:20 PM 0
Share

One issue is that you are using Input.Get$$anonymous$$ey() ins$$anonymous$$d of Input.Get$$anonymous$$eyDown(). Get$$anonymous$$ey() returns true for every frame it is held down, so you are getting multiple $$anonymous$$ove() coroutines launched even for a brief key down.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jan 08, 2014 at 06:31 PM

I might do it this way:

 void Update() {
     if (Input.GetKeyDown(KeyCode.H)) {
         StartCoroutine(Move(HomePos.transform.position, 3.0f, gameObject.transform));
     }
 }

 IEnumerator Move(Vector3 newPos, float time, Transform target) {
     float speed = (target.position - newPos).magnitude / time; 

     while (target.position != newPos) {
         transform.position = Vector3.MoveTowards (target.position, newPos, Time.deltaTime * speed);
         yield return null;
     }                                              
 }

Note I've eliminated the start position and just assume the current position is the start position.

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 isse26 · Jan 09, 2014 at 02:54 PM 0
Share

That helped the movement but now when I am trying to move agan it will constantly try to move back to HomePos. Is it possible to make it stop moving after 3s?

avatar image robertbu · Jan 09, 2014 at 03:26 PM 0
Share

I'm not sure what problem you are facing. The coroutine will quit when the object reaches the 'newPos' destination which will be in the specified amount of time. Show me the code you are using, and I can spot the problem. As a guess, I'd day you are repeatedly calling '$$anonymous$$ove' rather than calling it only once. There are other ways to solve this problem that don't involve coroutines that might be more flexible for you.

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

18 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

Related Questions

Instantiate Particles, WaitForSeconds and continue 1 Answer

Execute IEnumerator In 1 Frame 4 Answers

Fire rate for gun script, c# (not u/s) 1 Answer

WWW Request runs in Editor but not in Webplayer 1 Answer

IEnumerator only called once 3 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