手机上的Unity 2D滑动延迟

我正在尝试制作2D无限亚军游戏,但滑动操作无法正常进行。 滑动时会有明显的延迟,只有在我停止触摸并抬起手指后才发生滑动。

如何修改脚本以使滑动更负责,并在手指位于屏幕上时滑动?

我希望有人可以提供帮助。

void Update()
{
    if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        startSwipePoz = Input.GetTouch(0).position;
    }
    if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
    {
        endSwipePoz = Input.GetTouch(0).position;

        if (endSwipePoz.x < startSwipePoz.x && transform.position.x > -1.83f)
        {
            StartCoroutine(Move("left"));
        }
        if (endSwipePoz.x > startSwipePoz.x && transform.position.x < 1.83f)
        {
            StartCoroutine(Move("right"));

        }
    }    
}

private IEnumerator Move(string flySide)
{
    switch(flySide)
    {
        case "left":
            flyTime = 0f;
            startPoz = transform.position;
            endPoz = new Vector3(startPoz.x - 1.83f,transform.position.y,transform.position.z);

            while (flyTime < flyDuration)
            {
                flyTime += Time.deltaTime;
                transform.position = Vector2.Lerp(startPoz,endPoz,flyTime / flyDuration);
                yield return null;
            }
            break;

        case "right":
            flyTime = 0f;
            startPoz = transform.position;
            endPoz = new Vector3(startPoz.x + 1.83f,flyTime / flyDuration);
                yield return null;
            }
            break;
    }
}
yanglu06550132 回答:手机上的Unity 2D滑动延迟

一个简单的代码修复可能看起来像这样(我还将您的输入读入了Update方法的开头,而不是多次调用)。

Touch touch;
void Update()
{
    touch = Input.GetTouch(0);
    if(Input.touchCount > 0 && touch.phase == TouchPhase.Began)
    {
        startSwipePoz = touch.position;
    }
    else(Input.touchCount > 0)
    {
        endSwipePoz = touch.position;

        if (endSwipePoz.x < startSwipePoz.x && transform.position.x > -1.83f)
        {
            StartCoroutine(Move("left"));
        }
        if (endSwipePoz.x > startSwipePoz.x && transform.position.x < 1.83f)
        {
            StartCoroutine(Move("right"));

        }
    }    
}

您的代码之前不起作用的原因是因为您正在等待TouchPhase.Ended在更新对象的位置之前。您也可以添加对TouchPhase的检查。将其移至您的代码中并使其工作相同。

本文链接:https://www.f2er.com/3121953.html

大家都在问