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.

56 lines
1.2 KiB

  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. private Vector3 move = new Vector3(1, 0.0f ,0.0f);
  12. // Use this for initialization
  13. void Start () {
  14. startPosition = transform.position;
  15. if (!startRight) {
  16. transform.Rotate (Vector3.up * 180);
  17. direction *= -1;
  18. }
  19. }
  20. // Update is called once per frame
  21. void Update () {
  22. Vector3 curPosition = transform.position;
  23. move = new Vector3(1, 0.0f ,0.0f);
  24. move.x *= speed;
  25. move *= direction;
  26. if (curWait == 0) {
  27. if (Mathf.Abs(startPosition.x - curPosition.x) < patrolDistance)
  28. transform.position += move;
  29. else
  30. curWait = waitTime;
  31. } else if (curWait == 1) {
  32. transform.Rotate (Vector3.up * 180);
  33. direction *= -1;
  34. transform.position -= move;
  35. curWait--;
  36. } else
  37. curWait--;
  38. }
  39. void OnTriggerStay2D(Collider2D other) {
  40. if (other.tag == "Player") {
  41. Debug.Log(other.name);
  42. other.transform.parent.transform.position += move;
  43. }
  44. }
  45. }