- Home /
Simultaneous Multitouch Sends The Touch Over and Over
I'm having an issue with multitouch for my game, which consists of multiple modes. The basic premise of the whole game is to hit square pads in a grid as they light up (as a test of reflexes, reaction time etc). In some game modes where multitouch is enabled, if the player simultaneously makes two touches OR if they touch two or more fingers down, it appears to send the touch command over and over and over, spamming the pads (which flash red if they were unlit and touched).
Is there a way I can cancel previous touches once a new touch is made? Or anything I can do to avoid multiple simultaneous touches on the screen to spam the pads like they are doing? Cheers for the help.
Here is my touch input code, the first half of which handles mouse clicks and non-multi touch modes.
// Touch inputs and multitouch when enabled
if(Input.touchCount < 2){ // Code for detecting mouse input, primarily for testing
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray.origin, ray.direction,hit)){
if(Input.GetMouseButtonDown(0)) {
InputHandler(hit); // Function handling all registered pad hits
}
}
}else if(multiTouch) { // Code for detecting multiple touch inputs
var touchMax : int = 4; // Max 4 touches for multitouch modes to avoid spamming
for(var i = 0; i < touchMax; i++){
ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
if(Physics.Raycast(ray.origin, ray.direction,hit)){
InputHandler(hit); // Function handling all registered pad hits
}
}
}
Answer by Windemere · Jan 17, 2014 at 03:45 PM
I ended up figuring this out through a lot of trial and error fiddling with TouchPhase.Stationary and trying to disable touches from accessing InputHandler if they were stationary, but it ended up being a much more simple fix, just but putting my for loop code within a TouchPhase.Began if statement; that way, it won't spam my InputHandler after it's first instance.
Here's the code for any onlookers:
function Update() {
if(Input.touchCount < 2) { // Code for detecting mouse input, primarily for testing
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray.origin, ray.direction,hit)){
if(Input.GetMouseButtonDown(0)) {
InputHandler(hit); // Function handling all registered pad hits
}
}
}else if(multiTouch){ // Code for detecting multiple touch inputs
for(var i = 0; i < Mathf.Clamp(Input.touchCount, 0, 4); i++) {
ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
if(Input.touches[i].phase == TouchPhase.Began) { // If this is the first instance of this touch phase
if(Physics.Raycast(ray.origin, ray.direction,hit)) {
InputHandler(hit); // Function handling all registered pad hits
}
}
}
}
}