Vector3.MoveTowards issue

Hello, I have a question about “Vector3.MoveTowards”
I’m trying to do something like after I click the mouse button the box will from current position and move to target position and the target position. The target position x I want every time I click the mouse button it will +10f there. below is my code:

    public float characterMovmentSpeed;
    public Vector3 targetposition;
    public Vector3 currentPosition;

    // Use this for initialization
    void Start()
    {


    }

    // Update is called once per frame
    void Update()
    {
        move();
    }

    void move()
    {
        if (Input.GetMouseButtonDown(0))
        {
            float step = characterMovmentSpeed * Time.deltaTime;
            currentPosition = this.transform.position;
            targetposition.x = this.transform.position.x;
            targetposition.y = this.transform.position.y;
            targetposition.z = currentPosition.z + 10f;
            this.transform.position = Vector3.MoveTowards(currentPosition, targetposition, step);

        }
    }

the result is the box just moves very very tiny something like 0.1somrthing.
do anyone have solution for it?

Many Thanks

Your problem is that MoveTowards() will only work for that frame in this case because GetMouseButtonDown() will only work for that click, thus it will move on that frame.

You need to add some kind of second condition to ensure it moves until it hits the target without changing the target during that time.

Perhaps something like this would suffice?

 void move()
 {
     if (Input.GetMouseButtonDown(0))
     {
         currentPosition = this.transform.position;
         targetposition.x = this.transform.position.x;
         targetposition.y = this.transform.position.y;
         targetposition.z = currentPosition.z + 10f;
     }
     if(transform.position != targetPosition)
     {
         float step = characterMovmentSpeed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(currentPosition, targetposition, step);
     }
 }

94524-1click.png
94525-2click.png

here you go, every click it just move a bit