Swipe and Hold to move character .

Hi all ,

I am trying to move my character by following gesture : User swipes on screen ( this swipe determines which direction character is suppose to move ) and then without lifting the finger holds it down on the screen . When user holds it down i want it to keep looping (calling the move command with specific direction ) until user takes his finger off the screen . I tried using touchphase.Stationary instead of touchphase.ended which i am using in my current script , the results were wonky and weird and not perfect and i am clueless as to where to start. I hope somebody from here will b able to help me out .

using UnityEngine;
using System.Collections;

public class SwipeDetector : MonoBehaviour 
{
	private float fingerStartTime  = 0.0f;
	private Vector2 fingerStartPos = Vector2.zero;

	private bool isSwipe = false;
	private float minSwipeDist  = 30.0f;
	private float maxSwipeTime = 0.5f;

	public GameObject player;
	public PlayerMovement playerMovementScript;

	void Start(){

		

	}

	void Update()
	{

#if UNITY_EDITOR 

		if (Input.GetKeyDown (KeyCode.A)) {
			if(playerMovementScript.enabled == true){
				playerMovementScript.left = true;
			}
			

		}else if (Input.GetKeyDown (KeyCode.S)) {
			if(playerMovementScript.enabled == true){
				playerMovementScript.down = true;
			}
		

		}else if (Input.GetKeyDown (KeyCode.D)) {
			if(playerMovementScript.enabled == true){
				playerMovementScript.right = true;
			}
			

		}else if (Input.GetKeyDown (KeyCode.W)) {
			if(playerMovementScript.enabled == true){
				playerMovementScript.top = true;
			}
			

		}

#else

		if (Input.touchCount > 0 && Time.timeScale > 0.0f) 
		{

			foreach (Touch touch in Input.touches) 
			{

				switch (touch.phase) 
				{

				case TouchPhase.Began:
					/* this is a new touch */
					isSwipe = true;
					fingerStartTime = Time.time;
					fingerStartPos = touch.position;
					break;

				case TouchPhase.Canceled:
					/* The touch is being canceled */
					isSwipe = false;
					break;

				case TouchPhase.Ende:

					float gestureTime = Time.time - fingerStartTime;
					float gestureDist = (touch.position - fingerStartPos).magnitude;

					if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist) {

						Vector2 direction = touch.position - fingerStartPos;
						//Vector2 swipeType = Vector2.zero;
						int swipeType = -1;
	//						if (Mathf.Abs(direction.normalized.x) > 0.9)
	//						{
	//
	//							if (Mathf.Sign(direction.x) > 0) swipeType = 0; // swipe right
	//							else swipeType = 1; // swipe left
	//
	//						}
	//						else if (Mathf.Abs(direction.normalized.y) > 0.9)
	//						{
	//							if (Mathf.Sign(direction.y) > 0) swipeType = 2; // swipe up
	//							else swipeType = 3; // swipe down
	//						}
	//						else
	//						{
						// diagonal:
						if (Mathf.Sign (direction.x) > 0) {

							if (Mathf.Sign (direction.y) > 0)
								swipeType = 4; // swipe diagonal up-right
							else
								swipeType = 5; // swipe diagonal down-right

						} else {

							if (Mathf.Sign (direction.y) > 0)
								swipeType = 6; // swipe diagonal up-left
							else
								swipeType = 7; // swipe diagonal down-left

						}

						//						}

						switch (swipeType) {

						case 0: //right
	//							swipeDirection.GetComponent<Text>().text = "right";
							break;


						case 1: //left
	//							swipeDirection.GetComponent<Text>().text = "left";
							break;

						case 2: //up
	//							swipeDirection.GetComponent<Text>().text = "up";
							break;

						case 3: //down
	//							swipeDirection.GetComponent<Text> ().text = "down";
							break;

						case 4: //up right
//							swipeDirection.GetComponent<Text> ().text = "upright";
							playerMovementScript.right = true;
							break;
						case 5: //down right
//							swipeDirection.GetComponent<Text>().text = "downright";
							playerMovementScript.down = true;
							break;

						case 6: //up left
//							swipeDirection.GetComponent<Text>().text = "upleft";
							playerMovementScript.top = true;
							break;

						case 7: //down left
//							swipeDirection.GetComponent<Text>().text = "downleft";
							playerMovementScript.left = true;
							break;

						}

					}

					break;

				}

			}

		}
		#endif
	}

}

Sometimes all you need is some alone time and brain power to find the solution which in my case was pretty easy . Here is the final script :

using UnityEngine;
using System.Collections;

public class SwipeDetector : MonoBehaviour 
{
	private float fingerStartTime  = 0.0f;
	private Vector2 fingerStartPos = Vector2.zero;

	private bool isSwipe = false;
	private float minSwipeDist  = 30.0f;
	private float maxSwipeTime = 10f;

	public GameObject player;
	public PlayerMovement playerMovementScript;

	bool canInvoke = true;


	void Start(){


	}

	void Update()
	{

#if UNITY_EDITOR 

		if (Input.GetKeyDown (KeyCode.A)) {
			if(playerMovementScript.enabled == true){
				playerMovementScript.left = true;
			}
			
		}else if (Input.GetKeyDown (KeyCode.S)) {
			if(playerMovementScript.enabled == true){
				playerMovementScript.down = true;
			}
			
		}else if (Input.GetKeyDown (KeyCode.D)) {
			if(playerMovementScript.enabled == true){
				playerMovementScript.right = true;
			}
			
		}else if (Input.GetKeyDown (KeyCode.W)) {
			if(playerMovementScript.enabled == true){
				playerMovementScript.top = true;
			}
			
		}

#else

		if (Input.touchCount > 0 && Time.timeScale > 0.0f) 
		{

			foreach (Touch touch in Input.touches) 
			{
				if (touch.phase == TouchPhase.Began){
					isSwipe = true;
					fingerStartTime = Time.time;
					fingerStartPos = touch.position;
				}

				if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {


					float gestureTime = Time.time - fingerStartTime;
					float gestureDist = (touch.position - fingerStartPos).magnitude;

					if (canInvoke && isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist) {

						canInvoke = false;
						Invoke ("invokeMovement", .4f);

						Vector2 direction = touch.position - fingerStartPos;
						//Vector2 swipeType = Vector2.zero;
						int swipeType = -1;
						//						if (Mathf.Abs(direction.normalized.x) > 0.9)
						//						{
						//
						//							if (Mathf.Sign(direction.x) > 0) swipeType = 0; // swipe right
						//							else swipeType = 1; // swipe left
						//
						//						}
						//						else if (Mathf.Abs(direction.normalized.y) > 0.9)
						//						{
						//							if (Mathf.Sign(direction.y) > 0) swipeType = 2; // swipe up
						//							else swipeType = 3; // swipe down
						//						}
						//						else
						//						{
						// diagonal:
						if (Mathf.Sign (direction.x) > 0) {

							if (Mathf.Sign (direction.y) > 0)
								swipeType = 4; // swipe diagonal up-right
					else
								swipeType = 5; // swipe diagonal down-right

						} else {

							if (Mathf.Sign (direction.y) > 0)
								swipeType = 6; // swipe diagonal up-left
					else
								swipeType = 7; // swipe diagonal down-left

						}

						//						}

						switch (swipeType) {

						case 0: //right
//							swipeDirection.GetComponent<Text>().text = "right";
							break;


						case 1: //left
//							swipeDirection.GetComponent<Text>().text = "left";
							break;

						case 2: //up
//							swipeDirection.GetComponent<Text>().text = "up";
							break;

						case 3: //down
//							swipeDirection.GetComponent<Text> ().text = "down";
							break;

						case 4: //up right
//							swipeDirection.GetComponent<Text> ().text = "upright";
							playerMovementScript.right = true;
							break;
						case 5: //down right
//							swipeDirection.GetComponent<Text>().text = "downright";
							playerMovementScript.down = true;
							break;

						case 6: //up left
//							swipeDirection.GetComponent<Text>().text = "upleft";
							playerMovementScript.top = true;
							break;

						case 7: //down left
//							swipeDirection.GetComponent<Text>().text = "downleft";
							playerMovementScript.left = true;
							break;

						}

					}

				} else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) {
					canInvoke = true;
				}
				
			}

		}
		#endif
	}

	public void invokeMovement(){

		canInvoke = true;

	}
}