Problem with waypoints (problably Yield instruction?)

I’m making waypoints for my game, the AI is supposed to walk until the point, and when it’s close enough, wait a few seconds and walk to next one

the problem is that it walks to the first waypoint, and then it rotates and looks at the second and don´t move, no matter how many waypoints there is

here´s some code, it´s called in the Update Function

function movement()
{

				
		var relativeVector= nextPosition - transform.position;	
		var desiredRotation= Quaternion.LookRotation(relativeVector);
		turnStrenght = Mathf.Min(rotationSpeed*Time.deltaTime, 1);
		transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, turnStrenght);
		
		
		var angleToTarget = Vector3.Angle(nextPosition, transform.forward);
		
		if(angleToTarget < 50)
		{
			
			transform.Translate(0,0,velocidade*Time.deltaTime);
					}
		
		var distanceToDestination = Vector3.Distance(nextPosition, transform.position);
	
		if(radiusToDestination > distanceToDestination)
		{
						yield goToNextWaypoint();
					}

}



function goToNextWaypoint()
{
	
	currentPoint ++;
	print(currentPoint);
	if(currentPoint > (patrolPoints.length - 1))
	{
		
		currentPoint = 0;
	}
	
	nextPosition = patrolPoints[currentPoint];
	nextPosition.y = transform.position.y;
	yield WaitForSeconds(2);
	
	
}

i tried adding a flag to the movement funtion so that it only works after the whole function is finished, but that didn´t work either

any ideas guys?

thanks in advance

Did you try debugging the values you calculate to see if they are what you expect?

Looking at the angleToTarget calculation, you need to use two vectors that are relative to the character. transform.forward already is but nextPosition is relative to the global origin. This will give you a wrong angle and it might never get lower than 50 degrees.

You sir, are awesome

Thanks, the angle was in fact returning values such as 120, and more and i never thought it was the angle because it printed the log when it walked as soon as it hit the waypoint and it was collapsing

oops!

ill try to figure the solution for the angles, if i need help with it ill give a ring

thanks again