- Home /
Problem with Input.touchCount
I have a problem with Input.touchCount changing to 0 before I release my finger from the screen. I'll hold my finger on one of my move spots on the screen, and the transform will move. After about ~15 seconds Input.touchCount will change back to 0 (with my finger still in the same position) causing my moveVector to go to (0,0,0). I will stop moving at this time, and I don't want that. I can't seem to find if touches have a expiration time, which might be what is causing this. Any help would be appreciated. Here is my code for moving.
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
Vector3 moveVector = new Vector3(0,0,0); //Move Vector is added to my transform every frame
Vector2 touchVector = new Vector2(0,0); //Where the touch is on the screen
public float moveSpeed = 1.0f;
private float screenWidth;
private float screenHeight;
private Transform myTransform;
// Use this for initialization
void Start () {
screenWidth = Screen.width;
screenHeight = Screen.height;
myTransform = transform;
}
// Update is called once per frame
void Update () {
DetectTouches();
transform.position += moveVector * Time.deltaTime;
myTransform = transform;
}
public void DetectTouches(){
if(Input.touchCount == 0){
moveVector = new Vector3(0,0,0);
}
else if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began){
touchVector = Input.GetTouch(0).position; //Just used to display on the GUI for debuging
//Move to the left
if(Input.GetTouch(0).position.x >= 20 && Input.GetTouch(0).position.x <= 120 &&
Input.GetTouch(0).position.y >= 75 && Input.GetTouch(0).position.y <= 175){
moveVector.x = -moveSpeed;
//transform.Translate(-moveSpeed * Time.deltaTime, 0 , 0);
}
//Move to the right
else if(Input.GetTouch(0).position.x >= 125 && Input.GetTouch(0).position.x <= 225 &&
Input.GetTouch(0).position.y >= 75 && Input.GetTouch(0).position.y <= 175){
moveVector.x = moveSpeed;
//transform.Translate(moveSpeed * Time.deltaTime, 0 , 0);
}
//Move to the front
else if(Input.GetTouch(0).position.x >= 20 && Input.GetTouch(0).position.x <= 225 &&
Input.GetTouch(0).position.y >= 180 && Input.GetTouch(0).position.y <= 230){
moveVector.z = moveSpeed;
//transform.Translate(0, 0 , moveSpeed * Time.deltaTime);
}
//Move to the back
else if(Input.GetTouch(0).position.x >= 20 && Input.GetTouch(0).position.x <= 225 &&
Input.GetTouch(0).position.y >= 20 && Input.GetTouch(0).position.y <= 70){
moveVector.z = -moveSpeed;
//transform.Translate(0, 0 , -moveSpeed * Time.deltaTime);
}
}
//else if(Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended){
//moveVector = new Vector3(0,0,0);
//}
}
void OnGUI(){
GUI.Box(new Rect(20, screenHeight - 175, 100, 100), "<");
GUI.Box(new Rect(125, screenHeight - 175, 100, 100), ">");
GUI.Box(new Rect(20, screenHeight - 230, 205, 50), "^");
GUI.Box(new Rect(20, screenHeight - 70, 205, 50), "B");
//Next is just used to help debug
GUI.Label(new Rect(10, 10, 500, 25), touchVector.ToString() + " Transform: " + myTransform.position + " MoveVector: " + moveVector + " " + Input.touchCount);
}
}
Your answer
Follow this Question
Related Questions
What is the difference between iPhoneInput & Input ?? 1 Answer
Why can't I press both my touch controls at the same time? 1 Answer
Infuriating bug: Android build occasionally dropping touch input. 0 Answers
Input.touchCount won't return 2 or more on iOS. 1 Answer
Input.touches vs Input.GetTouch 1 Answer