Vector3.Lerp Question

The scene has 3 cubes in a horizontal line. The middle cube has the script, and there is also an empty gameobject in its place, which is pos1. When I run the code, I want the middle cube to travel to the left cube, than to the right cube. The first direction is being overridden however, and when I run the code, the cube starts at the left most cube and travels to the right most cube, instead of traveling to both locations. How can I make the cube go to both locations, one after the other?

var pos1 : Transform;
var pos2 : Transform;
var pos3 : Transform;

function Update () {

transform.position = Vector3.Lerp(pos1.position, pos2.position, .1 * Time.time);

transform.position = Vector3.Lerp(pos2.position, pos3.position, .1 * Time.time);

}

Perhaps this will help you out: Unity - Scripting API: Mathf.PingPong

this is probably not the best way

var pos1 : Transform;
var pos2 : Transform;
var pos3 : Transform;
var pos2boolean : boolean = false; 
 
function Update () {
 	if(transform.position == pos2.position){
 		pos2boolean = true;
 	}
 	if(pos2boolean == false){
		transform.position = Vector3.Lerp(pos1.position, pos2.position, .1 * Time.time);
	 }
	 else{
		transform.position = Vector3.Lerp(pos2.position, pos3.position, .1 * Time.time);
	 }
}

Both answers are very helpful, thank you very much guys. Is there a way to use some sort of collision method, like when the object collides with a position (other gameobject), it starts on a new path to a new position?

sure, just make the pos2 box to a trigger and add this skipt to it in the update loop

function OnTriggerEnter (other : Collider) {
    if(other.gameObject.tag == "test"){
    	other.GetComponent(SkriptName).pos2boolean = true;
    	}
}

you also need to add the tag “test” to your mouving box and you can delete:

    if(transform.position == pos2.position){

        pos2boolean = true;

    }

also change SkriptName to the name of the skript in the box.

you should read some basic java skipt guids than you can easly do stuff like that yourself :wink:

thanks that seems like what I am looking for. I am doing a tutorial series and just try and branch off to make each example a little more complicated and then I get stuck. Thanks for the awesome info guys.