Touch Hold for Power

Hey guys,

In my game, there is a cannon, and you use the accelerometer to tilt the angle of it. By touching the screen anywhere, and holding, I want to increment the power of the cannon so it shoots further and faster. Using the power float doesn’t trouble me, it is holding the finger on the screen and calculating based on how long it is hold.

I know I have to use some sort of TouchPhase.Stationary, but I’m not sure how. I’ve gotten this code and I’m not sure where or how to calculate power from it:

//Power Controller
		if (Input.touchCount == 1) {
			Touch touch = Input.GetTouch(0);
			if(touch.phase==TouchPhase.Began){
				newTouch = true;
				touchTime = Time.time;
				//Debug.Log("touchTime=" + touchTime);
			}else if (touch.phase == TouchPhase.Stationary){
				if(newTouch==true && Time.time-touchTime>1){
					//Debug.Log("longpress detected");
					newTouch = false;
					longPressDetected = true;
				}else if (newTouch == false && longPressDetected==false){ // began not detected
					newTouch = true;
					touchTime = Time.time;
					//Debug.Log("touchTime=" + touchTime);
				}
			}else{
				newTouch = false;
				longPressDetected = false;
				//Debug.Log("setting newTouch false");
			}
		}

Honestly I’m not even sure if the code works because the phone will not respond…

Here is what I use for apps:

function Update () {
	//Touch Screen***
	if(Input.touchCount > 0) {
		var tapCount = Input.touchCount;
		for(var i = 0; tapCount > i; i++) {			
			if(i < 1) {
				var touch = Input.GetTouch(i);
			}
			if(GetComponent(GUITexture)) {
				if(touch.phase == TouchPhase.Ended && guiTexture.HitTest(touch.position)) {
					//Call Up Function
					//Up();
				}
				else {
					if(touch.phase == TouchPhase.Stationary && guiTexture.HitTest(touch.position)) {
						//Call Hold Function
						//Hold();
					}
					else {
						if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) {
							//Call Down Function
							//Down();
						}
					}
				}
			}
		}
	}
}
function Hold() {
	//Increase your Power
}

This will work if pressing GUITextures. If your pressing a text, just change it to GUIText (obviously :)). I’m not sure it will work if your not pressing either though… like pressing into space, or on a gameObject. Maybe, (MeshRenderer)?

Also, you don’t have to call a function, you can just write it within the if statement. I just feel it is cleaner otherwise.