Waypoint enemy patrol trouble

I can’t figure out why my code isn’t working. I’ve searched all over the unity forums for how to do this but I couldn’t figure it out, here’s my code.

var enemy : Transform; 

var speed : float = 2.0; 

var waypoint1 : Transform;

var waypoint2 : Transform;

var waypoint3 : Transform;

var nextwaypoint1 : boolean;

var nextwaypoint2 : boolean;

var nextwaypoint3 : boolean;

function Update(){
	nextwaypoint1 = true;
	nextwaypoint2 = false;
	nextwaypoint3 = false;
	
	  if (nextwaypoint1 == true){
	  enemy.LookAt(waypoint1); 
      enemy.Translate(speed * Vector3.forward * Time.deltaTime); 
	  }
	  if (nextwaypoint2 == true){
	  enemy.LookAt(waypoint2);
	  enemy.Translate(speed * Vector3.forward * Time.deltaTime);
	  }
	  if (nextwaypoint3 == true){
	  enemy.LookAt(waypoint3);
	  enemy.Translate(speed * Vector3.forward * Time.deltaTime);
	  }
	  return;
}

function OnTriggerEnter(hit : Collider){
 if(hit.transform.tag == "Waypoint1"){
	  nextwaypoint1 = false;
	  nextwaypoint2 = true;
	  }
	  if(hit.transform.tag == "Waypoint2"){
	  nextwaypoint2 = false;
	  nextwaypoint3 = true;
	  }
}

I have the same problem, can’t get enemy moving from one waypoint to other and so on.

function Update(){
   nextwaypoint1 = true;
   nextwaypoint2 = false;
   nextwaypoint3 = false; 

// rest of code here //
}

What you do here neglects everything you do in your OnTriggerEnter, because you’re setting these values Every frame. So when your OnTriggerEnter gets called and changes those values, you Update basically says “oh thats nice and all, but I’ll just reset these because my writer told me to”.

Those values need to be set only at the start, so in your Start() would be a good idea.

Thank you. I actually changed my program eniterly and got it working. For some really good AI tutorials look up steamisM50 on youtube, he has some awesome videos.

Hm, I’ll check him out later, thanks.

Glad to see you have it working.