MoveTowards resets position

I want to move an object from his current position 1 unit along the y-axis like this:

 StartCoroutine(move(transform.position));
 
 IEnumerator move(Vector3 startPos)
 {
   transform.position = Vector3.MoveTowards(startPos, startPos + Vector3.up, walkSpeed);
   yield return new WaitForSeconds(3f);
 }

But when the function is recalled the object get’s teleported back to the initial position and doesn’t use it’s new position as startPos for the movement. Also when I do…

 Vector3 endPos = transform.position;
 Debug.Log(startPos+ ":" + (endPos));

…Unity tells me, that the startPos is the same as the endPos after the movement, even tough the object clearly moved. Please help me, I’ve been struggling with this since 4 hours

The line transform.position = Vector3.MoveTowards(startPos, startPos + Vector3.up, walkSpeed); gets called only 1 time in that setup. If you want that code to repeat itself every 3 seconds, you have to wrap it around a loop. Even after that you’ll only notice it move one time because the startPos doesn’t change after reached the destination. So, you’ll have to replace startPos with transform.position since it updates its position upon movement. In the end, the code would look something like this.

IEnumerator move()
	{
	while (true)
		{
			transform.position = Vector3.MoveTowards(transform.position, transform.position + Vector3.up, walkSpeed);
			yield return new WaitForSeconds(3f);
		}
	}

You can replace the condition in the while loop in however way you see fit in case you don’t want the object to move up indefinitely.

-Nova