Putting transform position on loop with WaitForSeconds Coroutine.

Hi everyone!

Was wondering if anyone could help. I’m trying to create a scene where red blood cells are freely flowing through a vessel. Cells are moving towards a target offscreen and I would like this to continue throughout the scene.

Here is my script so far:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {
public GameObject gameObject;
public Transform Target;
public float speed = 10f;
public float time = 1f;

private IEnumerator Move()
{
			while (true) {

					float step = speed * Time.deltaTime;

					gameObject.transform.position = Vector3.MoveTowards (transform.position, Target.position, step);
					yield return new WaitForSeconds (time);
					

			}
	}
		
		

 private void Update()
{
	StartCoroutine(Move());

}

}

Any help is greatly appreciated!

Kearley x

Change void Update() to Start(), this will start the coroutine only once, instead of starting a new coroutine every frame when Update is called.

Rename the Move coroutine, because it conflicts with the class name. ie, Change it to Move_CR() or something.

Change: “yield return new WaitForSeconds (time);” to “yield return null;” This will cause the coroutine to run once every frame, instead of every few seconds.

Unfortunately, it still isn’t working. Is there any possibility of making the target object position a trigger to restart the movement in a loop?