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;


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


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

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 : )