# How do I make a object move between two positions? and whats wrong with my code.

I have two empty game objects that are acting as my trigger points, wp1 (Waypoint 1) and wp2 (Waypoint 2), this is my code.

It’s moving from wp1 to wp2 with 2 seconds then jumping straight back to wp1 and repeating again.

I need to move the object between wp1 and wp2 so move from wp1 to wp2 then move back to wp1 and so on.

``````#pragma strict

// Transforms to act as start and end markers for the journey.
var startMarker: Transform;
var endMarker: Transform;

// Movement speed in units/sec.
var speed = 2.0;

// Time when the movement started.
private var startTime: float;

// Total distance between the markers.
private var journeyLength: float;

//How much distance has been covered so far
var fracJourney : float;

// Follows the target position like with a spring
var distCovered : float;

function Start ()
{

// Keep a note of the time the movement started.
startTime = Time.time;

journeyLength = Vector3.Distance(startMarker.position, endMarker.position);

distCovered = (Time.time - startTime) * speed;
}

function Update () {

// Distance moved = time * speed.
distCovered = (Time.time - startTime) * speed;

// Fraction of journey completed = current distance divided by total distance.
fracJourney = distCovered / journeyLength;

// Set our position as a fraction of the distance between the markers.
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
}

function OnTriggerEnter(objCollider : Collider)
{
if (objCollider.gameObject.name == "wp1")
{
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
}
else if (objCollider.gameObject.name == "wp2")
{
transform.position = Vector3.Lerp(endMarker.position, startMarker.position, fracJourney);
}

}
``````

You can do it with the following script

``````#pragma strict

// Transforms to act as start and end markers for the journey.
var startMarker: Transform;
var endMarker: Transform;

// Movement speed in units/sec.
var speed = 2.0;

// the wait time between waypoints
var waitTimeBetweenWaypoints : float = 0.25;

function Start ()
{
Patrol();
}

function Patrol()
{
while (true)
{
var time : float = 0;
var lerpValue : float;

// set the position of to the startMarker)
transform.position = startMarker.position;

// move object from startMarker to endMarker)
while ((transform.position != endMarker.position) && (time < speed))
{
time += Time.deltaTime;
lerpValue = (time / speed);
transform.position = Vector3.Lerp (startMarker.position, endMarker.position, lerpValue);
yield;
}
// set the position of to the endMarker)
transform.position = endMarker.position;
yield WaitForSeconds(waitTimeBetweenWaypoints);

time = 0;
// move object from endMarker to startMarker)
while ((transform.position != startMarker.position) && (time < speed))
{
time += Time.deltaTime;
lerpValue = (time / speed);
transform.position = Vector3.Lerp (endMarker.position, startMarker.position, lerpValue);
yield;
}
transform.position = startMarker.position;
yield WaitForSeconds(waitTimeBetweenWaypoints);
}
yield;
}
``````

[79994-patroltest.zip|79994] with a demo scene so you can see how it works.