|
|
- 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;
- private Vector3 move = new Vector3(1, 0.0f ,0.0f);
-
- // 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;
-
- 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--;
-
- }
- void OnTriggerStay2D(Collider2D other) {
- if (other.tag == "Player") {
- Debug.Log(other.name);
- other.transform.parent.transform.position += move;
- }
-
- }
- }
|