0

我有一个简单的 3D 篮球游戏,我正在使用 Unity(v 2017.3.1f1 64 位)和分屏多人游戏模式......基本上,如果你在屏幕的任一侧向上滑动,那一侧就会投篮,并且我进行了检查以确保无论您用多少手指滑动,每边只有一个球被射出,使用 Unity 触摸输入类中的手指 ID...一切似乎都工作正常,包括同时在两侧射击,但是它会在一侧随机停止工作,然后停止接受该侧的输入...有时它会在几秒钟后恢复,有时它只是在游戏的其余部分不起作用...我似乎无法一致地复制它,但它会经常发生......当我同时在每一侧使用多个手指时,它似乎也更频繁地发生,但是在每一侧都使用一根手指,或者有时只在一侧滑动时发生过……我确信代码可以写得更好,但我们希望让它尽可能简单和明确……我错过了吗这里有什么东西吗?...是否有可能使触摸阵列或类似的东西过载?..构建目标是 PC,并且在 Unity 编辑器和完整构建中都可以看到问题,在多个不同的触摸屏上,包括 IR 和电容屏。 ..提前感谢您的帮助...这是相关代码:构建目标是 PC 并且在 Unity 编辑器和完整构建中都可以看到问题,在多个不同的触摸屏上,包括 IR 和电容屏...提前感谢您的帮助...这是相关代码:构建目标是 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;

                }
            }
        }
    }

}
4

1 回答 1

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
                    }
                }
            }
        }
    }
}
于 2018-10-09T07:39:15.500 回答