Character Moves Without stopping

Hi guys,

Please, could somebody tell me what I’m doing wrong here?

I want the character to move towards just in his Z coordinate. So, I want to take his actual Z value and -1.5f to determine the range that he can move each time. However, he moves towards indefinitely.

Here’s how I’m doing it:

 public void Move()

{

Vector3 targetPosition;

targetPosition.y = 1.4f;

targetPosition.z = this.transform.position.z - 1.5f;

targetPosition.x = this.transform.position.x;

transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity);

}

How could I solve it?

May be you are calling your move function in update()? It gets recall every time and updates your current z position - 1.5f. Thats causing it to move infinite.

Set the z-position for your object outside the update function so that it doesnt get updated in every frame.
Currently, the z-position of your transform is becoming z-1.5f in every frame.