- Home /
Multi touch not working - What's wrong with this code?
Hi there,
I'm trying to get the Android/iOS controls up and running for my racing game, now that the desktop version is complete... I'm having issues with the multi-touch. For some reason, when I try steering, it stops accelerating until I release the steering button again. Probably something simple, I'm new to this touch stuff... any ideas?
EDIT: New code - multi-touch working but buggy - bools stay activated etc, so car tends to all of a sudden veer off the road or stop accelerating.
function OnGUI()
{
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase != TouchPhase.Ended && Input.GetTouch(i).phase != TouchPhase.Canceled)
{
if (GasButton.HitTest(Input.GetTouch(i).position))
{
bGasButtonPressed = true;
}
if (TurnLeft.HitTest(Input.GetTouch(i).position))
{
bLeftButtonPressed = true;
}
if (TurnRight.HitTest(Input.GetTouch(i).position))
{
bRightButtonPressed = true;
}
}
else
{
bGasButtonPressed = false;
bLeftButtonPressed = false;
bRightButtonPressed = false;
}
}
}
If you have 2 touches. Then lift one up, your else is going to trigger setting everything to false.
Hi Fattie, I was using that ins$$anonymous$$d of TouchPhase.Stationary as if the player moves their finger a bit, that would cancel it, no?
@PAEvenson: Haha, that'll learn me to code while I'm tired and sick :P
no, just completely leave out the line that says "If (Input...". just go immediately to the three hit text clauses. cheers !
Answer by Fattie · Sep 17, 2012 at 12:05 PM
Hello! Your general programming mistake here is ----->
You have to do this part FIRST .............
bGasButtonPressed = false;
bLeftButtonPressed = false;
bRightButtonPressed = false;
and ONLY THEN do this part,
for (var i = 0; i < Input.touchCount; ++i) .. and onwards
try it !
Secondly, be sure to answer the Q. i pose in the comment. Hope it helps so far.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081133im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make an object move in the direction of touch 0 Answers
Having an issue with getting my touch controls working 0 Answers
Touch joystick tutorials ? 0 Answers
android touch input 1 Answer
iOS - Mutlitouch Count 0 Answers