pseudo-code help

I have searched the forum and have not yet found anything that could help me.

I have an array of vector3 that I would like a gameobject to move towards, and then when it has reached that array[1] then move to array[2].

for(var i=0; i<array,length;){
transform.position += Vector3.lerp(transform.position, array[1], speed);
 if(transform.position == array[1]){
  i++;
}
}

That code will cause unity to freeze…understandingly. I just need some help on how to pseudo code what I need. Thanks in advance.

The following assumes that array is an array of transforms.

For Lerp and Slerp do not plug in the objects current position directly. create ‘to’ and ‘from’ variables to plug in.

Below declares to and from tranforms and then assigns them based on where in the array iteration you are. It then calls a coroutine that yields until the Lerp is complete.

var from : Transform;
var to : Transform;

function moveToNext()
{
    yield;
    transform.position = Vector3.lerp(from.position, to.position, speed); 
}

for (var i : int=0, i < array.length; i++)
{
    from = transform; // set our current transform to where our object is now
    to = array[i]; // set our destination transform to the next spot in the array. 
    yield moveToNext(); // move the position
}
function startMove() {
    var currentGoal = 0;
    while (array[currentGoal]) {
        yield Ani.Mate.To(transform, time, {"position": array[currentGoal]});
        currentGoal++;
    }
}

With AniMate

I really appreciate your help. That is exactly what I needed.

I have one more issue regarding the first reply. I am using that method (see below) and I obviously need to run the “findpoints” only once, but my problem is… strange things happen if I run CoRun in update, but if I dont use it in update, then it will instantly move to the end position without actually lerping it… for instance I want to move a unit smoothly through time to the end point, but if I use the update… then it will constantly run the for loop which messes up all the nodes. So what happens is the sphere does not just move to the next node, it just starts lerping there, and then it loops through the array again…

var target : Transform;
var nPoint : Vector3;
var array : Vector3[];
var from : Transform;
var vec : Vector3[];

function Start(){
	findPoints();
}
function MoveTo(){
	yield;
	var direction = nPoint - from.position;
	transform.rotation = Quaternion.Slerp(from.rotation,Quaternion.LookRotation(direction), Time.deltaTime);
	transform.position = Vector3.Lerp(from.position, nPoint, Time.deltaTime);
	transform.position.y = 0;
	
}

function CoRun(){ // if this is not in update it will only loop through once... and barely move... it doesnt seem to be yielding for the lerp to finish... because lerp is per frame correct?
for(var i=0;i<array.Length;i++){
	nPoint = array[i];
	from = transform;
	yield MoveTo();
}
}


function findPoints(){
var distance =  Vector3.Distance(transform.position, target.position);
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, distance)){
	var mx = hit.collider.bounds.min.x - 5;
	var mmx = hit.collider.bounds.max.x + 5;
	var mz = hit.collider.bounds.min.z - 5;
	var mmz = hit.collider.bounds.max.z + 5;
	
	vec = new Vector3[4]; // create 4 waypoints we can go to next - choices
	vec[0] = Vector3(mx,0,mz);
	vec[1] = Vector3(mmx,0,mz);
	vec[2] = Vector3(mx,0,mmz);
	vec[3] = Vector3(mmx,0,mmz);
	print("Hit" + hit.transform.name);
	var shortest : Vector3;
		for(var i=0; i<vec.Length; i++){
			shortest = vec[i]; // set the shortest path, not implemented yet
		}
		array = new Vector3[2];
		array[0] = shortest;
		array[1] = target.position;	
		CoRun();
}
else {
	print("noHit");
	array = new Vector3[1];
	array[0] = target.position;
	CoRun();
}}

Yikes! My apologies i didn’t write my function correctly.

function moveToNext()
{
   var timeFrac : float;
   while (timeFrac < speed) 
   {
    yield;

    timeFrac += Time.deltaTime;
    transform.position = Vector3.lerp(from.position, to.position, timeFrac/speed);
}

This way it will enter the while loop and as time frac aproaches your speed variable the lerp will move along with it.

Thanks for the response.

It seems I will have to fiddle around until I get what I need. Still having a little weird issues here and there…