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

时间:2018-10-08 22:23:28

标签: c# unity3d multiplayer split-screen

我有一个简单的3D篮球游戏,我正在使用带有分屏,多人游戏模式的Unity(v 2017.3.1f1 64位)...基本上,如果您向上滑动屏幕的任一侧,射击一个球,然后我检查一下以确保无论您用多少手指滑动,都可以使用Unity touch输入类中的手指ID来每侧只击出一个球...看起来一切正常,包括同时射击在两侧,但是它将随机停止在一侧上的工作,然后停止接受该侧的输入...有时它会在几秒钟后返回,有时在游戏的其余部分中不起作用...我可以似乎并没有始终如一地复制它,但是它会经常发生...当我同时在每一侧使用多个手指时,也似乎更经常发生,但是当我在每一侧使用一根手指时,或者有时在仅在一侧滑动。...我确定代码可以写得更好,但是我们希望将其保持为简单尽可能明确的...我在这里遗漏了一些东西吗?...是否可能使Touch数组或类似的东西过载了?..构建目标是PC,在Unity编辑器和完整构建中均会看到该问题,在多个不同的触摸屏上(包括IR和电容屏)...在此先感谢您的帮助...此处是相关代码:

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;

                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

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