can i use while loop in a method (or in recursive Courotine) for moving objecs?

hello every one
is it a true way for moving objects ?
and the speed of moving object does not change in different devices?

private void MoveObject()
{
    while (getTargetDistance(targetEnemy) > 0.20f)
    {
        projectile.transform.localPosition = Vector2.MoveTowards(projectile.transform.localPosition, targetEnemy.transform.localPosition, 5f * Time.deltaTime);
    }
}

or in recursive Courotine

IEnumerator Move(Vector3 target)
    {
		while(Mathf.Abs((target - transform.localPosition).y) > 0.20f)
        {
			Vector3 direction = target.y == topPosition.y ? Vector3.up : Vector3.down;
			transform.localPosition += direction * Time.deltaTime * speed;
			yield return null;
		}
		yield return new WaitForSeconds (0.5f);
		Vector3 newTarget = target.y == topPosition.y ? bottomPosition : topPosition;
		StartCoroutine (Move(newTarget));
	}

herey is my scripts reference for increasing player stamina, i hope it helps you

IEnumerator IncreaseStamina()
	{
		print ("Increase Stamina Started");

		while (stamina < maxStamina) {
			yield return new WaitForSeconds (0);
			stamina +=staminaIncreaseAmount;
			
		}
		StopAllCoroutines ();
	}

yield return new WaitForSeconds (0); is basically yield return null

For moving objects there are many ways:

  • CharacterController through the Move method [Players]
  • RigidBody through AddForce or velocity [Physics]
  • Any Transform through the transform.position [Custom]

You call those methods in different ways:

  • The Update, FixedUpdate or LateUpdate methods [Monobehaviour] which are called every frame or fixed frames
  • Coroutines

You should choose one method and a way dependig on your needs

Coroutines work this way:

private IEnumerator Cor_Something(Transform source, Transform target)
{
    bool MovementDone = false

    while (MovementDone)
    {
        Move()
        if (Distance(target, source) <= MinDistance)
            MovementDone = true;
        yield return new WaitForSeconds(n)/WaitForEndOfFrame()/WaitForFixedUpdate/etc
    }
}

You should NOT Instanciate your Coroutine inside your coroutine like you did !

In addition to all the other answers:

This kind of task is needed quite often. I’d recommend to use the free “LeanTween” assets which turns most of these tasks into a one-liner:

thanks every one for answering question.

i think find the answer after watching this video:

we can use Courotine for moving objects by a while loop.(See the following code) “yield return null;” means that Courotine will resume after the next update as a result content of while loop executed once per frame.

and Time.deltaTime work correctly

IEnumerator Move(Vector3 target)
{
    while (Mathf.Abs((target - transform.localPosition).y) > 0.20f)
    {
        Vector3 direction = target.y == topPosition.y ? Vector3.up : Vector3.down;
        transform.localPosition += direction * Time.deltaTime * speed;
        yield return null;
    }
    yield return new WaitForSeconds(0.5f);
    Vector3 newTarget = target.y == topPosition.y ? bottomPosition : topPosition;
    StartCoroutine(Move(newTarget));
}