Timing Move To Waypoint IEnumerator failing

Can anyone explain why I’m going wrong… I don’t understand :S

float Move_Speed = 0.053f;

	IEnumerator MoveToWaypoint()
	{
		float dist = Vector2.Distance(currentWaypoint.transform.position, transform.position);
		float time = dist / Move_Speed;

		for(float f = 0; f < time; f += Time.deltaTime)
		{
			Debug.Log(f + "  " + time);

			transform.Translate(((currentWaypoint.transform.position - transform.position).normalized) * Move_Speed);

			yield return new WaitForFixedUpdate();
		}
	}

It takes about 2 seconds to reach the target… but it calculates it as 27 seconds and wont stop moving back and forth over the target until 27 seconds is up. Can anyone advise?

Thanks

Probably it’s becouse of you using WaitFor__Fixed__Update and Time.deltaTime instead of Time.__fixed__Time?

You are calculating the time outside the loop, but your transform call is using the current position which changes every iteration. Try this instead:

float Move_Speed = 0.053f;

    IEnumerator MoveToWaypoint()
    {
        Vector2 deltaPos = currentWaypoint.transform.position - transform.position;
        float dist = deltaPos.length;
        Vector2 moveDir = deltaPos / dist;
        float time = dist / Move_Speed;

        for(float f = 0; f < time; f += Time.fixedTime)
        {
            Debug.Log(f + "  " + time);

            transform.Translate(moveDir * Move_Speed);

            yield return new WaitForFixedUpdate();
        }
    }