So I set up a simple waypoint script, where my AI patrols several waypoints, and when it reaches a waypoint, it rotates towards and moves to the next waypoint.
All I want to do is make it so that when the AI gets to a waypoint, he pauses for a moment, and THEN rotates and moves to the next waypoint. I've tried a couple things, like setting up timers and using WaitForSeconds, but I can't figure it out. Anyway, here is my script in its current state:
var waypoints : Transform[];
var moveSpeed = 3.0;
var rotationSpeed = 3.0;
var waypointStopRange = 1.0;
private var currentWaypoint : int;
function Update(){
PatrolWaypoints();
}
function PatrolWaypoints(){
if(currentWaypoint < waypoints.length){
var waypointDistance = Vector3.Distance(transform.position, waypoints[currentWaypoint].position);
var waypointDirection = waypoints[currentWaypoint].position - transform.position;
if(waypointDistance <= waypointStopRange){
currentWaypoint++;
}
else{
RotateTowards(waypointDirection);
MoveForward();
}
}
else{
if(waypointLoop){
currentWaypoint = 0;
}
}
}
function RotateTowards (position : Vector3) {
position.y = 0;
var targetRotation = Quaternion.LookRotation(position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
function MoveForward(){
var forward = transform.TransformDirection(Vector3.forward);
var moveDirection = forward * moveSpeed;
GetComponent (CharacterController).SimpleMove(moveDirection);
}
I've been trying to do the pause inside of if(waypointDistance <= waypointStopRange), because that is when he has reached currentWaypoint, and when currentWaypoint is incremented to the next. I cannot for the life of me figure out how to get him to stop moving for a few moments there. If I use yield WaitForSeconds(), he will just get stuck there indefinitely.