- Home /
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...
There could be an issue in how you are measuring touches. This code you have should be running in an object's update loop. Also, for good measure check for Input.touchCount >= 1
and print out a Debug.Log to show how many touches were measured. @Yoman
Answer by Lylek · Jun 30, 2014 at 05:36 AM
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.
Alright, that looks like it would work really well! I don't exactly know how I would test it atm because my cannon is being buggy itself! Once I get it fixed I'll let you know how it works. And about it touching anywhere, I can just instantiate a guitexture at the touch position so that's not a problem :)
Also would I need the Up(); and Down(); functions (obviously not with those names) but is it necessary for me to call that in my code?
No. Not if you don't need them. Treat them like On$$anonymous$$ouseDown, and On$$anonymous$$ouseUp functions. If you need something to happen as soon as you press down, and/or when you lift off the button.
Otherwise, feel free to delete that part of the code. :)
If you don't want to press a guiTexture or anything, like just anywhere on the screen, maybe just this will work?
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(touch.phase == TouchPhase.Stationary) {
//Increase Power
//Hold();
}
}
}
}
Haven't tried it though.
I'll try that. I tried inputing your code into the code I had for my GUITexture before, but I got a load of errors.
using UnityEngine;
using System.Collections;
public class Fire : $$anonymous$$onoBehaviour {
public float $$anonymous$$Power = 100;
public float powerValue;
void Update () {
//Touch screen applicable only:
if(Input.touchCount > 0) {
float tapCount = Input.touchCount;
for (float i = 0; tapCount > 1; i++) {
if(i < 1){
float touch = Input.GetTouch(i);
}
if(GetComponent(GUITexture)) {
if(touch.phase == TouchPhase.Ended && guiTexture.HitTest(touch.position)) {
//Call Up Function
GameObject.Find("GameController").GetComponent<GameController>().Fire(powerValue);
}
else {
if(touch.phase == TouchPhase.Stationary && guiTexture.HitTest(touch.position)) {
//Call Hold Function
Hold ();
}
else {
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) {
//This is when the touch has began
//Pretty much do nothing.. yet (later instantiate growing image at position)
}
}
}
}
}
}
}
void Hold() {
//Increase your Power
if (powerValue <= $$anonymous$$Power)
powerValue = $$anonymous$$Power;
else {
powerValue++;
}
}
}
And yes, I had converted your code from JS to C# because all of my other code is in C# and I like consistancy. There shouldn't be a problem though right?
Your answer
Follow this Question
Related Questions
Joystick trouble... 0 Answers
Question about Unity Touch Interface For Android 1 Answer
How can I make a thumbstick with this code? HELP 1 Answer
GUI.Button Touch Input Problem 1 Answer