using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class Patrol : MonoBehaviour {
|
|
|
|
public float patrolDistance;
|
|
public int waitTime;
|
|
public float speed;
|
|
public bool startRight;
|
|
|
|
public int direction = 1;
|
|
private Vector3 startPosition;
|
|
private int curWait = 0;
|
|
|
|
// Use this for initialization
|
|
void Start () {
|
|
startPosition = transform.position;
|
|
if (!startRight) {
|
|
transform.Rotate (Vector3.up * 180);
|
|
direction *= -1;
|
|
}
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
Vector3 curPosition = transform.position;
|
|
Vector3 move = new Vector3(1, 0.0f ,0.0f);
|
|
|
|
move.x *= speed;
|
|
move *= direction;
|
|
|
|
|
|
if (curWait == 0) {
|
|
if (Mathf.Abs(startPosition.x - curPosition.x) < patrolDistance)
|
|
transform.position += move;
|
|
else
|
|
curWait = waitTime;
|
|
} else if (curWait == 1) {
|
|
transform.Rotate (Vector3.up * 180);
|
|
direction *= -1;
|
|
transform.position -= move;
|
|
curWait--;
|
|
} else
|
|
curWait--;
|
|
|
|
}
|
|
}
|