﻿using UnityEngine;
using System.Collections;

public class Joysticks : MonoBehaviour {
	private Transform button;
	private bool isPress;
	public static float h = 0;
	public static float v = 0;
	// Use this for initialization
	void Awake () {
		button = transform.Find ("Button");
	}

	void OnPress(bool isPress){
//		Debug.Log ("Onpressed!!");
		this.isPress = isPress;
		if (!isPress) {
			//reset position
			button.localPosition = Vector3.zero;
			h = 0;
			v = 0;
		}
	}

	// Update is called once per frame
	void Update () {
		if (isPress) {
			Vector2 touchPos = UICamera.lastTouchPosition;
			touchPos -= new Vector2 (91, 91);
			float distance = Vector2.Distance (Vector2.zero, touchPos);
			if (distance > 73) {
				touchPos = touchPos.normalized * 73;
			}
			button.localPosition = touchPos;

			h = touchPos.x / 73;
			v = touchPos.y / 73;
		}


	}
}
