Lerp Movement Pauses after frame

Hey everyone, I’ve been trying to sort this out for the last 6 hours. Maybe you can help me. In my Update function I have an input cmd:

`if(Input.GetButtonDown("Walk")){
 	CurrentUnit = true;
 	Play();
 	CurrentUnit = false;
    }
`

Then in my Play function:

`function Play () {
//Debug.log(Team1Unit1Origin.GetType);
//collect all waypoint game objects
var waypoints = new Array (GameObject.FindGameObjectsWithTag("Waypoint"));
	//loop through waypoint array
	for (i = 0; i < waypoints.length; i++){	
		//variable for waypoint vector
		var newposition = waypoints*.transform.position;*
*		//set time scale*
_		t = Time.deltaTime * 5;_
*		//move unit to first waypoint in array*
_		Team1Unit1.transform.position = Vector3.Lerp(Team1Unit1.transform.position, newposition, Time.DeltaTime * 5);_
*	}
`*

The problem I’m having is that the Unit stops after each frame, so I have to keep tapping the Walk button for it to continue.
I’m not quit sure what I’m doing wrong, if anyone could help that would be great.

If you want the object Team1Unit1 to move to all waypoints in sequence when you press “walk”, Play must be a coroutine where the object Lerps from the current position to the new waypoint at a defined speed, then change the waypoint and Lerps to it, and so on till the last one. The variable walking is set at the beginning of Play, and only cleared at the end - it’s used to avoid other Play cycles being initiated while one is running.

I changed the creation of the waypoints array to Start, because Find operations are too slow (the docs say we should use them only at Start/Awake).

var speed: float = 3; // speed = 3 m/s
private var waypoints: GameObject[];

function Start(){
  waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
}

function Update(){
  if (Input.GetButtonDown("Walk")){
    Play(); // starts Play cycle and returns immediately
  }
}

private var walking = false;

function Play () {
  if (walking) return; // don't start a new cycle until this one has finished
  walking = true; // cycle started
  //loop through waypoint array
  for (i = 0; i < waypoints.length; i++){
    // get next waypoint
    var newposition = waypoints*.transform.position;*
 *// save start position*
 *var oldPos = Team1Unit1.transform.position;*
 *// calculate factor to walk at defined speed*
 *var spd = speed / Vector3.Distance(oldPos, newposition);*
 *for (var t:float=0; t<1;){ // sweep from oldPos to newPosition*
 _t = Time.deltaTime * spd; // ensures motion at defined speed_
 *// move object a little each frame*
 *Team1Unit1.transform.position = Vector3.Lerp(oldPos, newposition, t);*
 *yield; // return here next frame*
 *}*
 *}*
 *walking = false; // cycle ended*
*}*
*
*

HaHa yeah I just got it to work after finding this post:

http://answers.unity3d.com/questions/160549/click-on-object-and-move-with-lerp.html

Seriously after like 6 hours of trying different things. I know how to program, but I don’t know how games work :stuck_out_tongue:

I’m not sure if I can populate the waypoint array in the function start though. The player is able to spawn as many waypoints as they like. Then the play function gathers the waypoints and moves the player. Think Frozen Synapse. Perhaps I can populate the array on the user pick? Then send that array to the MainGameScript.

Thanks so much for your help, I’m loving this!