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

Unity - split screen touch input randomly stops working mid-game

我有一个简单的 3D 篮球游戏,我正在使用 Unity(v 2017.3.1f1 64 位)和分屏多人模式...基本上,如果您在屏幕的任一侧向上滑动,一侧将射出一个球,我进行了检查以确保无论你用多少手指滑动,每侧都只射出一个球,使用 Unity 触摸输入中的手指 ID class...一切似乎工作正常,包括同时在两侧拍摄,但它会随机停止一侧的工作,然后停止接受该侧的输入......有时它会在几秒钟后恢复,有时它只是对其余部分不起作用游戏......我似乎无法始终如一地复制它,但它会经常发生......当我同时在每一侧使用多个手指时,它似乎也更经常发生,但发生在用一根手指每一侧,或者有时只在一侧滑动……我确定代码可以写得更好,但我们希望保持简单le 和 explicit as possible...我在这里遗漏了什么吗?...是否有可能重载 Touch 阵列或类似的东西?..构建目标是 PC 并且在 Unity 编辑器和完整构建中都看到了这个问题, 在多种不同的触摸屏上,包括红外线和电容屏...在此先感谢您的帮助...这是相关代码:

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;

                }
            }
        }
    }

}

无论射速情况如何,当适当的触摸结束或取消时,您应该重置 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
                    }
                }
            }
        }
    }
}