- Home /
Input.touchCount returning wrong number
I've been venturing into mobile development lately and am running into an issue with touch input. The touchCount and TouchPhase seem to be returning the wrong information. Here's some skeleton code:
mainCharacter.js
function Update() {
var input:float = touchInputScript.getInput();
}
touchInputScript.js
var inputDirection:float = 0.0;
var inputThreshold:float = 5.0;
function getInput():float {
if(Input.touchCount > 0) {
var touch = Input.touches[0];
Debug.Log(Input.touchCount);
if(touch.phase == TouchPhase.Moved) {
if (touch.deltaPosition.x < -inputThreshold)
inputDirection = -1;
else if (touch.deltaPosition > inputThreshold)
inputDirection = 1;
else
inputDirection = 0;
}
} else {
inputDirection = 0;
}
return inputDirection;
}
This usually works as expected, but frequently it will report the touchCount as 1 even when nothing is touching it at all. This 'ghost finger' is always reported as TouchPhase.Stationary. This has me stumped and is very frustrating. Should I just be running the getInput() function inside of touchInputScript's update() function, storing the state and then checking the variable from mainCharacter.js?
Thanks in advance for any suggestions. :)
Edit
Well, I reduced my code to the following:
function Update() {
print(Input.touchCount);
}
And if I scrub my finger back and forth at a decent speed, touchCount will begin to register 2 (or sometimes more) touches. Once my finger is removed, it will continue to register a single finger on screen. It's very strange and very frustrating.
Happens on both an iPad and an LG G2X (Android.) It seems more prone to happen with the Unity Remote, but happens (far less frequently) on the hardware.
Where is the touch variable that you are using here? The iPad input functionality has always worked great for us. We have had some issues with it on Android, mostly related to mul$$anonymous$$ch on certain devices.
You might have some luck using a for loop to iterate over all the touches and grab their state info.
int moveTouchId = -1;
for(int i = 0; i < Input.touches.Length; i++) { Touch t = Input.GetTouch(i);
if(t.phase == TouchPhase.Began && moveTouchId == -1)
{
moveTouchId = t.fingerId;
}
if(t.phase == TouchPhase.$$anonymous$$oved && t.fingerId == moveTouchId)
{
//do your movement code
}
if(t.phase == TouchPhase.Ended && t.fingerId == moveTouchId)
{
moveTouchId = -1;
}
}
We've used stuff like this and haven't had any problems.
I edited my first post to show where I grab the touch variable.
I am having the same problem with a nexus 5 android 5.0