C# Unity-分屏触摸输入在游戏中随机停止工作

C# Unity-分屏触摸输入在游戏中随机停止工作,c#,unity3d,multiplayer,split-screen,C#,Unity3d,Multiplayer,Split Screen,我有一个简单的3D篮球游戏,我正在使用Unity(v 2017.3.1f1 64位)和分屏多人模式…基本上,如果你在屏幕的任何一侧向上滑动,那一侧将射出一个球,我会检查以确保无论你用多少个手指滑动,每侧都只射出一个球,在Unity touch输入类中使用手指ID…一切似乎都正常,包括在两侧同时拍摄,但它会随机停止一侧的工作,然后停止接受该侧的输入…有时几秒钟后会返回,有时它在游戏的其他部分不起作用…我似乎无法始终如一地复制它,但它会经常发生…当我同时在每一侧使用多个手指时,它似乎也会发生得更频繁

我有一个简单的3D篮球游戏,我正在使用Unity(v 2017.3.1f1 64位)和分屏多人模式…基本上,如果你在屏幕的任何一侧向上滑动,那一侧将射出一个球,我会检查以确保无论你用多少个手指滑动,每侧都只射出一个球,在Unity touch输入类中使用手指ID…一切似乎都正常,包括在两侧同时拍摄,但它会随机停止一侧的工作,然后停止接受该侧的输入…有时几秒钟后会返回,有时它在游戏的其他部分不起作用…我似乎无法始终如一地复制它,但它会经常发生…当我同时在每一侧使用多个手指时,它似乎也会发生得更频繁,但在每一侧使用一个手指时,或者有时只在单面滑动…我确信代码可以写得更好,但我们希望尽可能简单明了…我是否遗漏了一些东西?…是否可能是触摸阵列过载或类似的情况?.构建目标是PC,而Unity editor和完整构建中都可以看到这个问题,在多个不同的触摸屏上,包括红外屏幕和电容式屏幕…提前感谢您的帮助…以下是相关代码:

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;

                }
            }
        }
    }

}
int屏幕中点
int finId1=-1;
int finId2=-1;
void Start()
{
Input.multiTouchEnabled=真;
屏幕中点=屏幕宽度/2;
}
无效更新()
{
如果(Input.touchCount>0)
{
foreach(Input.touch中的var touch)
{
if(touch.phase==TouchPhase.start)
{
//左半屏
如果(touch.position.x屏幕中点和终点==-1)
{
p2StartPos=触摸位置;
p2StartTime=Time.Time;
finId2=touch.fingerId;
}
}
else if(touch.phase==TouchPhase.end | | touch.phase==TouchPhase.cancelled)
{
如果(touch.fingerId==finId1&&Time.Time>p1NextFire)
{
p1NextFire=时间。时间+燃速;
p1EndTime=Time.Time;
p1EndPos=触摸位置;
p1DeltaSwipe=p1EndPos-p1StartPos;
如果(p1DeltaSwipe.y>0)
{
//p1射击代码在这里
}
finId1=-1;
}
else if(touch.fingerId==finId2&&Time.Time>p2NextFire)
{
p2NextFire=时间。时间+燃速;
p2EndTime=Time.Time;
p2EndPos=触摸位置;
p2DeltaSwipe=p2EndPos-p2StartPos;
如果(p2DeltaSwipe.y>0)
{
//p2射击代码在这里
}
finId2=-1;
}
}
}
}
}

在适当的触摸结束或取消时,无论火速情况如何,都应重置finId1和finId2。试试这个:

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
                    }
                }
            }
        }
    }
}
void Update()
{
如果(Input.touchCount>0)
{
foreach(Input.touch中的var touch)
{
if(touch.phase==TouchPhase.start)
{
//左半屏
如果(touch.position.x屏幕中点和终点==-1)
{
p2StartPos=触摸位置;
p2StartTime=Time.Time;
finId2=touch.fingerId;
}
}
else if(touch.phase==TouchPhase.end | | touch.phase==TouchPhase.cancelled)
{
如果(touch.fingerId==finId1)
{
finId1=-1;
p1EndTime=Time.Time;
p1EndPos=触摸位置;
p1DeltaSwipe=p1EndPos-p1StartPos;
如果(p1DeltaSwipe.y>0&&Time.Time>p1NextFire)
{
p1NextFire=时间。时间+燃速;
//p1射击代码在这里
}
}
else if(touch.fingerId==finId2)
{
finId2=-1;
p2EndTime=Time.Time;
p2EndPos=触摸位置;
p2DeltaSwipe=p2EndPos-p2StartPos;
if(p2DeltaSwipe.y>0&&Time.Time>p2NextFire)
{
p2NextFire=时间。时间+燃速;
//p2射击代码在这里
}
}
}
}
}
}

在适当的触摸结束或取消时,无论火速情况如何,都应重置finId1和finId2。试试这个:

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
                    }
                }
            }
        }
    }
}
void Update()
{
如果(Input.touchCount>0)
{
foreach(Input.touch中的var touch)
{
if(touch.phase==TouchPhase.start)
{
//左半屏
如果(touch.position.x屏幕中点和终点==-1)
{
p2StartPos=触摸位置;
p2StartTime=Time.Time;
finId2=touch.fingerId;
}
}
else if(touch.phase==TouchPhase.end | | touch.phase==TouchPhase.cancelled)
{
如果(touch.fingerId==finId1)
{
finId1=-1;
p1EndTime=Time.Time;
p1EndPos=触摸位置;
p1DeltaSwipe=p1EndPos-p1StartPos;
如果(p1DeltaSwipe.y>0&&Time.Time>p1NextFire)
{
p1NextFire=时间。时间+燃速;
//p1