# waypoint system help

Hi,
i’ve been having trouble figuring out how to add a way for the rigid-body to stop at a specific waypoint then continue on to the next waypoint.

here is the code so far.

``````var WalkSpeed = 1.0;
var speed : float = 5.0;
private var currentWaypoint : int;
var loop = true;
var waypoint : Transform[];

function Start(){
animation["walk"].wrapMode = WrapMode.Loop;

}

function Update(){

animation["walk"].speed = WalkSpeed;

if(currentWaypoint < waypoint.length){

var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;

if(moveDirection.magnitude < 1){
currentWaypoint++;

}
else{
velocity = moveDirection.normalized*speed;
animation.Play("walk");
}
}

else{
if(loop){
currentWaypoint = 0;

}
else{
velocity = Vector3.zero;
}

}

rigidbody.velocity = velocity;
transform.LookAt(target);
}
``````

What exactly is the problem? At a quick glance this code should work for moving between waypoints. Do you simply need the object to stop as it approaches a waypoint and then continue on after a set period of time?

If that’s the case then you can just add a check in this section:

``````if(moveDirection.magnitude < 1){
currentWaypoint++;
}
``````

Before you increment the currentWaypoint value simply set a value to the current Time.time. Afterwords check to see if the difference between the current Time.time and your base start value exceeds your pause duration. If it does, continue on to the next waypoint.

``````if(moveDirection.magnitude < 1){
if (curTime == 0)
curTime = Time.time;
if ((Time.time - curTime) >= pauseDuration) {
currentWaypoint++;
curTime = 0;
}
else {
velocity = Vector3.zero;
}
}
``````

Setting the velocity straight to zero is obviously not the best way to handle a “pause”, but it works for the sake of this example. You would probably want to smoothly move the value down toward zero depending on the situation (and consequently, push up the waypoint detection range from 1 to something higher).