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));
	}

4 Answers

4

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

You wait for 0 seconds? why? and why do you stop ALL the coroutines afterwards?

Okay now it works. But it shrinks to fast, i would like it to be about 30 seconds to shrink. And then shrink in to (3, 1, 3,) I am sorry if i seem to be ungrateful of your help and just seem like your were nothing, that is not what i mean. I my intension was to ask if you could help me something more, not to say that your job was not done well enough :). EDIT: "It shrinks too fast."

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 !

Hello there, No worries, it's perfectly all right to ask. In this case, the shrink already works based on the "duration" parameter. All you need to do is call ScaleToTarget(new Vector3(3.0f, 1.0f, 3.0f), duration);, and replace "duration" with the time you want the process to take in seconds. In my example above I set it to 2.5 seconds, but you could just as easily give it 50. Hope that helps! ~LegendBacon

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:

I din't think using a plug in will let him learn the behaviour of the engine, it may confuse the understanding

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));
}