Using yield, need help.

What im trying to do is to have a patrolling robot, that when it reaches the one of the waypoints of his way, it stops and rotate around, like a patroll system.

The thing is, that when it comes back from yield, it increases the currentWaypoint thousands of time. Why is that happening? The var ‘currentWayPoint’ is the index of my waypoint vector.

if (goingForth  currentWayPoint < wayPoint.length-1)
		{						
			yield StartCoroutine("patrolAtStop");
			currentWayPoint++;
		}

function patrolAtStop()
{
	transform.Rotate(0,30,0);
	yield WaitForSeconds(2);
	transform.Rotate(0,-30,0);	
}

The ‘patrolAtStop’ method is not complete, i was just making some tests.

Any insights?

a couple of things could be happening:

  1. you are incrementing currentWayPoint by 1 thousands of times.
  2. you are overwriting currentWayPoint somewhere else in the code.

find all references to currentWayPoint in the code and put a print statement to see what is going on. Or post your entire sript.

In case you don’t know, the 3d platformer tutorial have guards patrolling around. I don’t think it has waypoints, but you may still find something useful there:

maybe try updating currentWayPoint within your patrolAtStop function … and where do you update the goingForth variable?

I think im not increasing it some place else.

Here it goes the whole code:

function Start () 
{
	// Usado quando tinha o caminho tosqueragem
	initialPosition = transform.position;	
	player = GameObject.Find("Player2");
	target = player.transform;
}

function Update () 
{			
	// Linha de Debug para vermos onde que estamos mirando
	frontPosition = transform.TransformDirection(Vector3.forward);	
	Debug.DrawRay(transform.position, frontPosition * 5, Color.green);
		
	// Fica verificando se existe o objeto especificado pela layerMask 
	// na direção que estamos mandando o nosso raio, no caso 'front'
	if (Physics.Raycast(transform.position, frontPosition, 7, layerMask))
	{		
		canSeePlayer = true;		
	}
	
	if (!canSeePlayer)
	{		
		WayPointSystem();
	}
	else
	{	
		FollowPlayer();
	}	
}

function FollowPlayer()
{	
	transform.LookAt(player.transform.position);	
	transform.Translate(Vector3.forward * Time.deltaTime * speed * 5);
}

function WayPointSystem()
{	
	var target = wayPoint[currentWayPoint].position;
	transform.LookAt(target);
	
	var moveDirection = target-transform.position;
	// Retorna o tamanho do vetor, poderia usar a função que calcula distância		
	if (moveDirection.magnitude > 1)
	{
		transform.Translate(Vector3.forward * Time.deltaTime * speed);		
	}
	else
	{
		if (goingForth  currentWayPoint < wayPoint.length-1)
		{						
			yield StartCoroutine("patrollAtStop");
			currentWayPoint++;
		}
		else
		{		
			goingForth = false;
			if (currentWayPoint > 0)
			{
				currentWayPoint--;
			}
			else
			{
				goingForth = true;
			}		
		}		
	}
}

function patrollAtStop()
{
	transform.Rotate(0,30,0);
	yield WaitForSeconds(2);
	transform.Rotate(0,-30,0);		
}

Any ideas, please?

you didn’t post the whole script, so I can’t say for certain, but my guess is:

if (goingForth  currentWayPoint < wayPoint.length-1)

is true all the time. So you every update frame you are incrementing currentWayPoint.
put a print statement here:

if (goingForth  currentWayPoint < wayPoint.length-1) 
      {                   
         yield StartCoroutine("patrollAtStop"); 
         currentWayPoint++; 
         print(currentWayPoint);
      }

see if it increases by one every frame

Its not true all the time, check it:

if (goingForth  currentWayPoint < wayPoint.length-1)
		{				
			//yield StartCoroutine("patrollAtStop");
			currentWayPoint++;
			print(currentWayPoint);
		}
		else
		{		
			goingForth = false;
			if (currentWayPoint > 0)
			{
				currentWayPoint--;
			}
			else
			{
				goingForth = true;
			}

I see not only if the goingForth is true, but also if the number is lower then the size of the array.

Then thing is, when i use the yeld functions, appear that when it comes back from the ‘freezing’ it computes the time it was stoped in a short amount of time and crashes my methods. I cant see why thought!

It appears that it doesnt run the whole update function again…

And the thing is that is increasing it two times a frame!!

:frowning:

I don´t know what´s happening either, but I really like this comment on the code

“// Usado quando tinha o caminho tosqueragem”

I think if you use a state machine, with a variable to define the state, you could split the whole code in patrol and idle while npc is realigning to some direction.

well, I cannot check it since I don’t know what
goingForth and wayPoint.length-1 are.
Again, you can print these.
If the print statement print(currentWayPoint) is executing every frame, then this condition is true and it explains your problem.
You don’t want to be calling StartCoroutine(“patrollAtStop”) every frame.

You can have a loop that goes through all waypoints like this:

function patrol(){
for (currentWayPoint = 0; currentWayPoint < wayPoints.length; currentWayPoint++){
   yield StartCoroutine("goToNextWayPoint");
   yield StartCoroutine("patrollAtStop");
}
}

function Start(){
   patrol();
}

or something along the lines…