- Home /
VERY weird touch responses. BUG?
Have I found a bug in unity? i wrote this script to detect swipes / tap to be calculated by the distance between the beginning of the touch and the end of the touch.If I move left or right more than 100 units, then it is a swipe, other wise its a tap.
The problem is that when I tap the screen, it understands that it is a tap, but it also think that it is a swipe.
So when I tap the screen, console:
Debug.Log message : tap
Debug.Log message : swipe
//////
Debug.Log message : tap
Debug.Log message : swipe
///////
But when I swipe, it is fine. console:
Debug.Log message : swipe
///////
Debug.Log message : swipe
///////
Here is the code:
// I put this code in a GUI function because I use some gui labels, and I don't want to put it in update() because it might cause error if it updates while it is in the middle of the touch.
//float distance = 0
foreach (Touch t in Input.touches) {
if (t.phase == TouchPhase.Began) {
distance = t.position.x;
}
if (t.phase == TouchPhase.Ended) {
distance = distance - t.position.x;
if (distance > 100 || distance < -100) {
Debug.Log ("swipe");
} else {
Debug.Log ("tap");
}
distance = 0;
}
}
any idea?
Answer by tanoshimi · Aug 06, 2014 at 08:40 AM
I suspect your problem comes because you've put it in OnGUI, which is called multiple times per frame - for each "tap" that ends you're therefore subtracting the position several times and always recording a distance of < -100.
Don't ever include code logic in OnGUI - only put in GUI code.
@tanoshimi thanks for your reply. I forgot about that. Where should i include those code then? If i put it in the update function, it will crash right? Because it would be the same as a while loop inside an update?
Let me quote the documentation:
Note also that the Input flags are not reset until "Update()", so its suggested you make all the Input Calls in the Update Loop.
Also if you think there might be some issues due to putting your code into Update then you might think of restructuring your code.
Your answer
Follow this Question
Related Questions
InputSystem simulated touchscreen stops working from v1.1.0 preview 1 0 Answers
Bug moving a character with onscreen Joystick prefab 0 Answers
smooth movement with rigidbody2D 1 Answer
Unity Getting Input from Horizontal axis while using buttons 1 Answer
Application in windowed mode - Why does finger-touch work with Unity4x and no longer with Unity5x? 0 Answers