using UnityEngine;
using System.Collections;

public class Ghost : MonoBehaviour
{
		public int i = 1;
		public Vector3[] targetPos;
		public Vector3 originalPos;
		public Vector3 currentVel;
		public float vel;

	public static bool isEnabled = false;

		void Start ()
		{
				
		}

	private bool isFirstTime = true;
		void FixedUpdate ()
		{
		if (!isEnabled)
						return;
		if (isFirstTime) {
			targetPos [0] = transform.position;
			originalPos = targetPos [0];
			//transform.LookAt (targetPos [i]);
			currentVel = new Vector3 (targetPos [i].x - originalPos.x, 0, targetPos [i].z - originalPos.z);
			currentVel.Normalize ();
			rigidbody.velocity = vel * currentVel;
			Debug.Log (vel * currentVel);
			isFirstTime = false;
				}
				if (Mathf.Abs (transform.position.x - originalPos.x) >= Mathf.Abs (targetPos [i].x - originalPos.x) && 
						Mathf.Abs (transform.position.z - originalPos.z) >= Mathf.Abs (targetPos [i].z - originalPos.z)) {
						originalPos = targetPos [i];
						if (i == targetPos.Length - 1) {
								i = 0;
						} else {
								i++;
						}
						//transform.LookAt (targetPos [i]);
						currentVel = new Vector3 (targetPos [i].x - originalPos.x, 0, targetPos [i].z - originalPos.z);
						currentVel.Normalize ();
						rigidbody.velocity = vel * currentVel;
				}
		}
}