'Unity2d: Object either moves too fast or too slow

void MoveSteps(float speed, int steps)
{
        isMoving = true;
        if (GameObject.Find("Camera").GetComponent<room>().playerNum == 1)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("clicked LMB");
                Vector2 mousePos = Input.mousePosition;
                mousePos = new Vector2(mousePos.x, mousePos.y);
                Vector2 worldPos = Camera.main.ScreenToWorldPoint(new Vector2());

                Debug.Log("COROUTINE: MoveSteps smooth steps process is called");
                int counter = 0;
                while (counter != steps)
                {
                    transform.position = Vector3.MoveTowards(transform.position, mousePos, 1);
                    // yield return new WaitForSecondsRealtime(0.0000001f * speed);
                    counter += 1;
                }
            }
        }
        isMoving = false;
}

Above is my code to smoothly move my square to the mouse. But this moves too fast.

IEnumerator MoveSteps(float speed, int steps)
{
        isMoving = true;
        if (GameObject.Find("Camera").GetComponent<room>().playerNum == 1)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("clicked LMB");
                Vector2 mousePos = Input.mousePosition;
                mousePos = new Vector2(mousePos.x, mousePos.y);
                Vector2 worldPos = Camera.main.ScreenToWorldPoint(new Vector2());

                Debug.Log("COROUTINE: MoveSteps smooth steps process is called");
                int counter = 0;
                while (counter != steps)
                {
                    transform.position = Vector3.MoveTowards(transform.position, mousePos, 1);
                    yield return new WaitForSecondsRealtime(0.0000001f * speed);
                    counter += 1;
                }
            }
        }
        isMoving = false;
}

Now it seems that there is a limit to the short-ness of the float in the WaitForSecondsRealtime. This moves slow and no matter how small, it caps at some number and then will not move faster. Any help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source