using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Touch_move : MonoBehaviour {
	public GameObject player;
	public Vector3 pos;
	public float speed;
	public GameObject object1;
	public GameObject text_frame;
	public string st;
	public Text txt;
	public bool a;

	// Use this for initialization
	void Start () {
		pos = player.transform.position;
		}


	IEnumerator PlayText(){

		foreach (char c in st) {

			txt.text += c;
			yield return new WaitForSeconds (0.5f);
		}

		if (txt.text.Length == st.Length) {

			a = false;
			object1 = null;
			txt.text = "";
			text_frame.SetActive (false);

		}

		print ("morteza");
	}

	void FixedUpdate(){
		if (object1) {
			if (!a) {
				if (object1.name == "zombie_frame") {
					st = "hello morteza";
					txt.text = "";
					if (player.transform.position.x == pos.x) {

						text_frame.SetActive (true);

						StartCoroutine ("PlayText");
						a = true;
					}


				}

			}
		}

		if (Input.touchCount == 1) {

			if (Input.GetTouch (0).tapCount == 1) {
				speed = 0.06f;
				Ray ray;
				ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);

				RaycastHit hit;

				if(Physics.Raycast(ray,out hit)){
					object1 = hit.collider.gameObject;
				

					if (hit.point.x < player.transform.position.x) {


						pos = new Vector3 (hit.point.x + 2, pos.y, pos.z);
					} else {

						pos = new Vector3 (hit.point.x - 2, pos.y, pos.z);
					}


					if (player.transform.position.x > pos.x) {

						player.transform.localScale = new Vector3 (-1.5970f, player.transform.localScale.y, player.transform.localScale.z);

					} else {

						player.transform.localScale = new Vector3 (1.5970f, player.transform.localScale.y, player.transform.localScale.z);
					}

				}



			}
			if (Input.GetTouch (0).tapCount == 2) {

				speed =0.16f;

				Ray ray;
				ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);

				RaycastHit hit;

				if(Physics.Raycast(ray,out hit)){

					print (hit.point);

					if (hit.point.x < player.transform.position.x) {


						pos = new Vector3 (hit.point.x + 2, pos.y, pos.z);
					} else {

						pos = new Vector3 (hit.point.x - 2, pos.y, pos.z);
					}


					if (player.transform.position.x > pos.x) {

						player.transform.localScale = new Vector3 (-1.5970f, player.transform.localScale.y, player.transform.localScale.z);

					} else {

						player.transform.localScale = new Vector3 (1.5970f, player.transform.localScale.y, player.transform.localScale.z);
					}

				}

			}

		}

		player.transform.position = Vector3.MoveTowards (player.transform.position, pos, speed);

	}
	// Update is called once per frame

}
