Reputation: 37
I have a simple 3D basketball game Im working on using Unity (v 2017.3.1f1 64 bit) with a split-screen, multiplayer mode...basically, if you swipe up on either side of the screen, that side will shoot a ball, and I put in a check to ensure no matter how many fingers you swipe with, only one ball gets shot out per side, using finger Ids in Unity touch input class...everything seems to work fine, including shooting simultaneously on both sides, however it will randomly stop working on one side and then it stops accepting inputs for that side...sometimes it comes back after a few seconds, sometimes it just doesnt work for the rest of the game...I can't seem to replicate it consistently, but it will happen quite often...it also seems to happen more often when I use multiple fingers on each side at the same time, but has happened using one finger on each side, or sometimes when only swiping on a single side....Im sure the code could be written better, but we wanted to keep it as simple and explicit as possible...am I missing something here?...is it possible it is overloading the Touch array or something like that?..the build target is PC and the issue is seen in both Unity editor and full builds, on multiple different touchscreens including IR and capacitive screens...thank you in advance for any help...here is the relevant code:
int screenMidPoint
int finId1 = -1;
int finId2 = -1;
void Start()
{
Input.multiTouchEnabled = true;
screenMidPoint = Screen.width / 2;
}
void Update()
{
if (Input.touchCount > 0)
{
foreach (var touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
//For left half screen
if (touch.position.x <= screenMidPoint && finId1 == -1)
{
p1StartPos = touch.position;
p1StartTime = Time.time;
finId1 = touch.fingerId;
}
//For right half screen
else if (touch.position.x > screenMidPoint && finId2 == -1)
{
p2StartPos = touch.position;
p2StartTime = Time.time;
finId2 = touch.fingerId;
}
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
if (touch.fingerId == finId1 && Time.time > p1NextFire)
{
p1NextFire = Time.time + fireRate;
p1EndTime = Time.time;
p1EndPos = touch.position;
p1DeltaSwipe = p1EndPos - p1StartPos;
if (p1DeltaSwipe.y > 0)
{
// p1 Shoot code is here
}
finId1 = -1;
}
else if (touch.fingerId == finId2 && Time.time > p2NextFire)
{
p2NextFire = Time.time + fireRate;
p2EndTime = Time.time;
p2EndPos = touch.position;
p2DeltaSwipe = p2EndPos - p2StartPos;
if (p2DeltaSwipe.y > 0)
{
// p2 Shoot code is here
}
finId2 = -1;
}
}
}
}
}
Upvotes: 0
Views: 935
Reputation: 71
You should reset finId1 and finId2 when appropriate touch ended or canceled regardless fire rate condition. Try this:
void Update()
{
if (Input.touchCount > 0)
{
foreach (var touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
//For left half screen
if (touch.position.x <= screenMidPoint && finId1 == -1)
{
p1StartPos = touch.position;
p1StartTime = Time.time;
finId1 = touch.fingerId;
}
//For right half screen
else if (touch.position.x > screenMidPoint && finId2 == -1)
{
p2StartPos = touch.position;
p2StartTime = Time.time;
finId2 = touch.fingerId;
}
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
if (touch.fingerId == finId1)
{
finId1 = -1;
p1EndTime = Time.time;
p1EndPos = touch.position;
p1DeltaSwipe = p1EndPos - p1StartPos;
if (p1DeltaSwipe.y > 0 && Time.time > p1NextFire)
{
p1NextFire = Time.time + fireRate;
// p1 Shoot code is here
}
}
else if (touch.fingerId == finId2)
{
finId2 = -1;
p2EndTime = Time.time;
p2EndPos = touch.position;
p2DeltaSwipe = p2EndPos - p2StartPos;
if (p2DeltaSwipe.y > 0 && Time.time > p2NextFire)
{
p2NextFire = Time.time + fireRate;
// p2 Shoot code is here
}
}
}
}
}
}
Upvotes: 2