【问题标题】:Unity 2D swipe latency on phone手机上的 Unity 2D 滑动延迟
【发布时间】:2020-03-07 11:31:19
【问题描述】:

我正在尝试制作 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, transform.position.y, transform.position.z);

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

【问题讨论】:

  • 为什么要使用string 参数来定义方向?我建议宁愿使用 int (1 / -1) 或 bool isRight (true / false) 甚至 enum MoveDirection { Right, Left }!其中任何一个都比传递和比较strings 更易于维护且不易出错:)

标签: android unity3d touch swipe


【解决方案1】:

对您的代码的简单修复可能如下所示(我还在 Update 方法的开头将您的输入读取移动到单个 .GetTouch 中,而不是多次调用它):

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.Moved 的检查并使其正常工作。

【讨论】:

  • 谢谢,现在滑动是有责任的,但现在玩家没有去应该去的固定位置。我更新了帖子并添加了移动脚本。
  • 游戏地图由3条车道组成,玩家可以向左或向右滑动。汽车从顶部驶来,玩家必须避开它们,我希望玩家留在车道中间
  • 我怀疑主要问题在于您的 flyDuration。话虽如此,您使用的是静态值(如 1.83f,我假设为 flyDuration)而不是游戏对象。我建议您使用游戏对象和变换来实现在位置之间移动玩家。例如,不是每次滑动时都计算 endPoz,而是将 3 个车道位置标识为变换并“选择”滑动时要调整到的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 1970-01-01
相关资源
最近更新 更多