Hi,
I’m making a multiplayer running game on android using C#.
Each player get’s half of the screen and uses his fingers to swipe over the screen to simulate running.
I’ve run into a big wall getting both players to be able to swipe simultaneously.
I’ve been using fingerId and checking where they tap to see which player is swiping where but many different ways haven’t been working out.
What am I doing wrong?
if (Input.touchCount > 0)
{
if (Input.touches[Input.touchCount -1].position.x < screenwidth.x)
{
if (AActive == false)
{
AActive = true;
touchA = Input.touches[Input.touchCount -1];
startPosA = Input.touches[Input.touchCount -1].position;
startTimeA = Time.time;
fingerA = Input.touches[Input.touchCount - 1].fingerId;
}
}
else
{
if (BActive == false)
{
BActive = true;
touchB = Input.touches[Input.touchCount -1];
startPosB = Input.touches[Input.touchCount -1].position;
startTimeB = Time.time;
fingerB = Input.touches[Input.touchCount - 1].fingerId;
}
}
switch (touchA.phase)
{
case TouchPhase.Began:
couldBeSwipeA = true;
//stuff
break;
case TouchPhase.Moved:
//stuff
break;
case TouchPhase.Ended:
endPosA = Input.touches[fingerA].position;
if (couldBeSwipeA == false)
{
AActive = false;
}
if (couldBeSwipeA)
{
swipeDistA = startPosA.x - endPosA.x;
swipeTimeA = Time.time - startTimeA;
if ((swipeTimeA < maxSwipeTime) && (swipeDistA > minSwipeDist))
{
//calculate speed
}
}
AActive = false;
break;
}
And the same happens for touchB.phase in a switch.
Thanks so much for all your time/help. It is solved. Wish I could give you 10 thumbs up :) I kept the AActive and BActive bools in my code as I didn't want the player to be able to swipe with more than 1 finger at the same time. I've reposted my new and complete code if you are interested. Thanks again!
– NGKrush1Glad to hear it! Good luck with the rest of your game.
– kmeboeOh, also -- your idea about disallowing multiple touches per side is a good one. If you want, you can probably do away with your bools, and just check for "touchA != -1" instead, since this should mirror the bool at all times. In this smallish algorithm, it's easy enough to see what's going on, so it's not a big deal in any case. In a larger design, a good "best practice" would be to use a method instead of a bool, like this: bool IsTouchAActive(){ return touchA != -1; }
– kmeboe