- Home /
Android multitouch problem
I've recently come across a problem regarding implemanting multitouch. I've looked at unity's own multitouch help but even when i'm copying the code and try it, it doesn't work.
This thread shows how to do it: http://answers.unity3d.com/questions/167750/how-does-multi-touch-work.html
//The good way;
var tapCount = Input.touchCount;
for ( var i = 0 ; i < tapCount ; i++ ) {
var touch = Input.GetTouch(i);
if( gasButton.HitTest (Input.GetTouch(i).position))
{
throttleForward = 1;
}else
{
throttleForward = 0;
}
if( rightButton.HitTest (Input.GetTouch(i).position))
{
stearC = 1;
}else
{
stearC = 0;
}
}
Now I copied the code and I am not able to press the gasbutton and rightbutton at the same time. I can only press one button at the same time. Isn't the for loop changing the i value between 1 - 2 and jumping between the touch positions and run the if statements? I've must have gotten it all wrong.
Never the less this code should work but the lines i have put into it is not right. I would be grateful if somebody could correct my mistakes
Answer by maccabbe · Jul 15, 2015 at 09:57 PM
I think you're right that the problem is that both values are changing for both touches. Right now what would occur if you tried to press both buttons is
touch 1 presses gassButton but not rightButton so it sets throttleForward to 1 and stearC to 0
touch 2 presses gassButton but not rightButton so it sets throttleForward to 0 and stearC to 1
so while both touch 1 and touch 2 are being processed it is done in such a way that touch 2 is overriding all effects of touch 1. Instead it would make sense to have the buttons in their default state of 0 and set to 1 only if a touch presses them. For instance
throttleForward = 0;
stearC = 0;
var tapCount = Input.touchCount;
for ( var i = 0 ; i < tapCount ; i++ ) {
var touch = Input.GetTouch(i);
if( gasButton.HitTest (Input.GetTouch(i).position))
{
throttleForward = 1;
}
if( rightButton.HitTest (Input.GetTouch(i).position))
{
stearC = 1;
}
}
Now what should happen is
throttleForward and stearC are both set to 0
touch 1 presses gassButton but not rightButton so it sets throttleForward to 1
touch 2 presses rightButton but not gassButton so it sets stearC to 1
now both throttleForward and stearC are 1
It doesn't help. These controls will be used for controlling vehicles. So i want to be able to throttle and turn at the same time. Now i notice as you said before that the second touch overrides the first one. But isn't the for loop supposed to change the i value (1-2) and jump from finger to finger every update and check the current finger's position and see if that finger's position is on one of the buttons? It would do it so fast that you would not notice that it actually only handles one finger at a time.
I don't see how this doesn't help. As I explained, the code allows you to activate both buttons at once using two different touches.
The for loop is used to go through all touches every update which is what you should be doing. It is possible to change it so it cycles through the touches and processes only one touch every update, but what then?
Best case scenario is that ins$$anonymous$$d of your input overriding each other every update you have your touches overriding each other every 2 updates. It'll cause other problems that you'll have to address in addition to the original problem which doesn't seem like it would actually be solved.
Oh! never $$anonymous$$d, I done goofed :) Forgot to change the code before building it. Thanks a lot :D
Your answer
Follow this Question
Related Questions
For loop is being totally ignored 1 Answer
"for" loop - DrawTexture problem. 0 Answers
Shorten code with FOR loop 1 Answer
For loop C#? 2 Answers
Using for as while. 3 Answers