【问题标题】:Unity Swipe Functionality IssueUnity 滑动功能问题
【发布时间】:2014-03-04 10:29:34
【问题描述】:

我在为游戏创建滑动控件时遇到了一些麻烦。我正在尝试重新创建游戏 Subway Surfers 的控件,其中在 Touch.Moved 阶段而不是 Touch.End 阶段检测到滑动。现在它大部分时间都有效,但偶尔(通常)垂直滑动将被视为水平滑动。 我已经提交了下面的代码,在此先感谢您的帮助。

       int swipeIndex = -1; //index of first detected swipe to prevent multiple swipes
       Vector2 startPos = Vector.zero; //starting position of touch
       //minSwipeDist is the distance in inches the player must swipe for a correct swipe
       for (int i = 0; i < Input.touchCount; i++) {
            Touch touch = Input.touches[i];
            switch (touch.phase) {
                case TouchPhase.Began:
                    if (swipeIndex == -1) {
                        swipeIndex = i;
                        startPos = touch.position;
                    }
                    break;
                case TouchPhase.Moved:
                    if(i != swipeIndex)
                        break;
                    Vector2 direction = touch.position - startPos;
                    //vertical swipe
                    if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y)) {
                        //swipe up
                        if ((touch.position.y - startPos.y) > minSwipeDist) {
                            //Process Up swipe
                            swipeIndex = -1;
                        }
                        //swipe down
                        else if ((touch.position.y - startPos.y) < -minSwipeDist) {
                            //Process down swipe
                            swipeIndex = -1;
                        }
                    }
                    //horizontal swipe
                    else {
                        //swipe left
                        if ((touch.position.x - startPos.x) < -minSwipeDist) {
                            //process left swipe
                            swipeIndex = -1;
                        }
                        //swipe right
                        else if ((touch.position.x - startPos.x) > minSwipeDist) {
                            //process right swipe
                            swipeIndex = -1;
                        }
                    }
                    break;
            }
        }

【问题讨论】:

    标签: c# unity3d touch swipe


    【解决方案1】:

    我认为您应该将代码从 TouchPhase.Moved 移动到 TouchPhase.Ended。 基本上你在 TouchPhase.Began 时得到接触点,然后你在 TouchPhase.Moved 中不断更新你的第二个点,然后在 TouchPhase.Ended 中处理这两个点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2020-01-31
      • 2016-02-09
      相关资源
      最近更新 更多