- Home /
Touch returns twice on Android
This is driving me insane. I thought I had it down, moving my raycasting code right into the Update function, but no luck. Whenever I touch a button on my android tablet (Nexus 7 2013), my tests show that it's been touched twice. No problem whatsoever with the editor. Any idea how this is happening? Here's the relevant code:
void Update () {
CheckTouches();
StaticIfNotInFocus(staticEnabled);
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
Ray ray = guiCamera.ScreenPointToRay(Input.GetTouch (0).position);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, guiLayerMask)){
testButton = "Hit Button: "+hit.collider.transform.parent.name;
WhichButtonPressed(hit.collider.transform.parent.name);
}
}
}
void CheckTouches(){
if(Input.GetButtonDown("Fire1")){
Ray ray = guiCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, guiLayerMask)){
testButton = "Hit Button: "+hit.collider.transform.parent.name;
WhichButtonPressed(hit.collider.transform.parent.name);
}
}
}
Answer by vividhelix · Nov 05, 2013 at 05:49 AM
Uhmm..comment out the CheckTouches call at line 2? Your code is doing a raycast twice.
To elaborate, in the editor, the mouse click will fire a "Fire1". On the device, you'll get both the touch code starting at line 4, but the touch will register as a "Fire1" as well resulting in the double event. In general, you'll want to safeguard stuff using the mouse with #if UNITY_EDITOR or whatever other platforms you'll be using a mouse on.
Doh. I didn't think about touches registering as clicks. Thanks for pointing that out :)
Your answer
Follow this Question
Related Questions
Unity Touch=2 commends? 1 Answer
Problem with Input.touchCount == 1 1 Answer
Scripts don't work when i try app 2 Answers
Tracking Touch Points on Screen for Mobile Devices 1 Answer
Main menu touch input for android 1 Answer