looping lerp between waypoints

Hello.

I am trying to get an object to smoothly interpolate and loop between 3 waypoints (which can be moved during gameplay). Am getting close, but there seems to be some significant framerate drop and inconsistent speeds occurring. Here is my code so far. Am a bit of a noob so this might be completely the wrong approach. Has anyone got any suggestions? Thanks.

var waypoint1 : Transform;
var waypoint2 : Transform;
var waypoint3 : Transform;

var i = 0.0;
var i2 = 0.0;
var i3 = 0.0;

var speedbar = 1.0;
var speedbar2 = 1.0;
var speedbar3 = 1.0;

function Start () {	 
DelayAndDo();
} 

function DelayAndDo () {

   var dist = Vector3.Distance(waypoint1.position, waypoint2.position);
   for (i = 0.0; i < 1.0; i += (speedbar * 0.1 * Time.deltaTime) / dist) {
   
     transform.position = Vector3.Lerp(waypoint1.position, waypoint2.position, i);
     yield;
     
     DelayAndDo2 ();
    
   }}
	
	
function DelayAndDo2 () {
	
   var dist2 = Vector3.Distance(waypoint2.position, waypoint3.position);
   for (i2 = 0.0; i2 < 1.0; i2 += (speedbar2 * 0.1 * Time.deltaTime) / dist2) {
    
     transform.position = Vector3.Lerp(waypoint2.position, waypoint3.position, i2);
     yield;
     DelayAndDo3 ();
      
}
}	

function DelayAndDo3 () {

   	  var dist3 = Vector3.Distance(waypoint3.position, waypoint1.position);
   for (i3 = 0.0; i3 < 1.0; i3 += (speedbar2 * 0.1 * Time.deltaTime) / dist3) {
   
     transform.position = Vector3.Lerp(waypoint3.position, waypoint1.position, i3);
     yield;  
}

}	
	
	
function Update (){
if (i3 >= 1.0){

DelayAndDo ();

}}

Did you look on the wiki at the AnimateAPI ?

The reason you’re having framerate issues is because hundreds of these things are being run at once. You don’t see it because only the last one executed every frame is visible. If you don’t want to rewrite your script, you can most likely fix your problems by reinitializing i, i2, and i3 at the beginning of DelayAndDo(). As of right now, as soon as i3 hits 1.0, you’re constantly executing a new loop without fresh values of these variables.

A rewrite using an array of transforms would be recommended, but somewhat longwinded to explain here.

Thanks for your suggestions StarManta and twintower31.
Will look into both solutions. Much appreciated.

Cheers.

I dont konw what you want, why put the DelayAndDo2() in the for loop??