You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
996 B

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Patrol : MonoBehaviour {
  4. public float patrolDistance;
  5. public int waitTime;
  6. public float speed;
  7. public bool startRight;
  8. public int direction = 1;
  9. private Vector3 startPosition;
  10. private int curWait = 0;
  11. // Use this for initialization
  12. void Start () {
  13. startPosition = transform.position;
  14. if (!startRight) {
  15. transform.Rotate (Vector3.up * 180);
  16. direction *= -1;
  17. }
  18. }
  19. // Update is called once per frame
  20. void Update () {
  21. Vector3 curPosition = transform.position;
  22. Vector3 move = new Vector3(1, 0.0f ,0.0f);
  23. move.x *= speed;
  24. move *= direction;
  25. if (curWait == 0) {
  26. if (Mathf.Abs(startPosition.x - curPosition.x) < patrolDistance)
  27. transform.position += move;
  28. else
  29. curWait = waitTime;
  30. } else if (curWait == 1) {
  31. transform.Rotate (Vector3.up * 180);
  32. direction *= -1;
  33. transform.position -= move;
  34. curWait--;
  35. } else
  36. curWait--;
  37. }
  38. }