# Player doesn't reach waypoint location

My player currently traverses the nodes in the Stack of Vector3 called currentPath. The problem is that it seems to do it in one frame. However when I change the moveTowards function from speed to speed*Time.deltaTime, it goes at the correct speed but it only goes halfway towards a node before starting to head for the next, but never fully reaches them.

``````Update()
{
if (currentPath != null && currentPath.Count > 0)
{
if (transform.position!=currentPath.Peek())
{
transform.position = Vector3.MoveTowards(transform.position, currentPath.Peek(), speed);
}
else
{
currentPath.Pop();
if (currentPath.Count == 0)
{
moving = false;
}
}
}
}
``````

I’ve tried adding a timer but it didn’t seem to work
Any help, I’d be really grateful.
Thank you!

MoveTowards is just as easy as lerp, it’s just a different way of accomplishing the same thing. Lerp will interpolate between 2 Vector3’s linearly and MoveTowards will just move towards a Vector3 position by the amount you pass as the last parameter. I think since you are constantly changing your end positions based on click positions, MoveTowards is a better bet, because if you change your target halfway through a Lerp, it is no longer linear and the speed will change unless you do extra calculations to compensate for the non linear movement.

If it is doing it all in one frame, your speed/maxDistanceDelta value is much to high. You should lower the speed value first of all and to make sure that you at least spend one frame at each waypoint you should check if speed is mor than the distance from the player to the waypoint and if it is, that frame, you should just move the length of distance rather than speed.

Cheers

I know this isn’t similar to what you have but it would be simpler to use lerps like this

``````public Transform[] points;
public float distanceTravelled;
public float speed;
void Update()
{
distanceTravelled += speed;
if(distanceTravelled < 1)
{
transform.position = Vector3.Lerp(points[0].position, points[1].position, distanceTravelled);
}
else if(distanceTravelled < 2)
{
transform.position = Vector3.Lerp(points[1].position, points[2].position, distanceTravelled-1);
}
//and so on.
}
``````

or use an int variable to manage the points if the path is only followed one way.

``````public int currentPoint;
void Update()
{
distanceTravelled += speed;
if(distanceTravelled < currentPoint+1)
{
transform.position = Vector3.Lerp(points[currentPoint].position, points[currentPoint+1].position, distanceTravelled-currentPoint);
}
else{
currentPoint++;
}
}
``````