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 Silvx · Dec 10, 2017 at 05:27 AM · movementvector3coroutinecoroutinesmovetowards

MoveTowards inside Coroutine

Hello, I'm kinda new with coroutines and this is giving me a big headache.

I have to move a character from its position to a target character, attack it and then go back to the initial position, all of this inside a coroutine. I'm using Vector3.MoveTowards with a yield return null inside (and after) the iteration of the loop but doesn't seem to work, the gameobject doesnt move. But! If I try it with an if in Update(), works like a charm. My code right now in the coroutine looks like this:


           GameObject myChar, destinyChar;

           Vector3 destPos = destinyChar.transform.position + destinyChar.transform.forward;

           myChar.transform.LookAt(destPos);

           while (Vector3.Distance(myChar.transform.position, destPos)>0.5f) {
           myChar.transform.position = Vector3.MoveTowards(myChar.transform.position, destPos, 5);
           yield return null;

            }


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 Bunny83 · Dec 10, 2017 at 12:55 PM

Well i don't see a specific problem here except those three:


  • You use "5" as movement delta in your MoveTowards. That means each frame it would be allowed to move up to 5 units towwards the target. So if the objects are 15 units apart you will reach your destination after "3 frames" . You usually want to use Time.deltaTime for any movement that should happen over multiple frames. So when using 5 * Time.deltaTime the character is allowed to move 5 units per second instead of 5 units per frame.

  • The next problem is that you calculate your "destPos" once before the loop. That means destPos won't change when your "destinyChar" moves.

  • Finally it's not clear where and when you start the coroutine. This is probably the most important detail that's missing.


Keep in mind that you can start the same coroutine multiple times. This would have bad effects in your case as two coroutine instances might work against each other.


If that's your whole code inside your coroutine that means once yout character has reached it's target the coroutine will terminate.


There's simply too much information missing to answer the question propetly. Please edit your question and add the missing information.


ps: Is there any reason why you don't just use this code in Update? Why do you want to use a coroutine? Should that be a one-time "command" like "move over here and stop" or do you want to keep myChar in front of "destinyChar" all the time?


Details, please

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 Silvx · Dec 24, 2017 at 06:44 AM

The problem was that I wasn't updating properly the target and origin positions therefore the characters didn't move accordingly.

I will post some code as soon as I have a moment to : )

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

119 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

Related Questions

Setting target position in Vector3.MoveTowards 2 Answers

Adding curve to Vector3.MoveTowards 1 Answer

Convert WASD to local rotation 1 Answer

Moving only if straight path dijkstra 1 Answer

How to make a 2D object move smoothly in a random direction for a set amount of time. 2 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