检测滑动而不会丢失点击的立即响应

时间:2019-01-18 16:07:19

标签: android unity3d gesture

我正在使用Unity创建一个Android游戏。只有三种方法可以控制角色的移动:

  • 点击屏幕的右半部分:跳到右侧
  • 点击屏幕左半部分:跳到左侧
  • 向上滑动:字符向前冲

从理论上讲,我知道我可以使用TouchPhase(开始,移动,固定和结束)来区分触摸。当只检测水龙头而不关心滑水时,我只是检查触摸阶段是否开始并使玩家跳动。在我的设备上感觉很快。

但是,由于我不得不考虑可能要进行滑动,因此,在检测到ThouchPhase.Ended之前,我无法启动跳转动作。这会导致响应速度非常慢,直到用户抬起屏幕手指时才跳动。

我尝试使用ThouchPhase.Moved和ThouchPhase.Stationary来模拟即时响应,但是我的解决方案在检测点击和滑动之间的差异方面非常糟糕:

Vector2 startTouchPosition;
Vector2 endTouchPosition;
Vector2 currentSwipe;

void Update()
{
    if (Input.touches.Length > 0)
    {
        for (int i = 0; i < Input.touchCount; i++)
        {
            Touch touch = Input.GetTouch(i);

            if (touch.phase == TouchPhase.Began)
            {
                //save began touch 2d point
                startTouchPosition = new Vector2(touch.position.x, touch.position.y);
            }

            if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
            {
                //save ended touch 2d point
                endTouchPosition = new Vector2(touch.position.x, touch.position.y);

                if (endTouchPosition.y - startTouchPosition.y < 5)
                {
                    if (touch.position.x > (Screen.width / 2))
                    {
                        JumpToRight();
                    }
                    else if (touch.position.x < (Screen.width / 2))
                    {
                        JumpToLeft();
                    }
                }
                else
                {
                    //create vector from the two points
                    currentSwipe = new Vector2(endTouchPosition.x - startTouchPosition.x, endTouchPosition.y - startTouchPosition.y);

                    //normalize the 2d vector
                    currentSwipe.Normalize();

                    //swipe upwards
                    if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
                    {
                        DashForward();
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是我使用的代码。我进行了测试,并且可以正常工作,但有时我注意到有一个延迟。让我知道它是否对您足够好。基本上,如果您不需要多点触摸,则无需进行所有触摸。您只需要开始和结束触摸阶段即可。

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class touch : MonoBehaviour {
private Vector2 startTouchPosition;
private Vector2 endTouchPosition;
private Vector2 currentSwipe;
public Text textbox;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    if (Input.touches.Length > 0)
    {

        Touch touch = Input.GetTouch(0);

        if (touch.phase == TouchPhase.Began)
        {
            //save began touch 2d point
            startTouchPosition = new Vector2(touch.position.x, touch.position.y);
        }

        if (touch.phase == TouchPhase.Ended)
        {
            //save ended touch 2d point
            endTouchPosition = new Vector2(touch.position.x, touch.position.y);
            //create vector from the two points
            currentSwipe = new Vector2(endTouchPosition.x - startTouchPosition.x, endTouchPosition.y - startTouchPosition.y);

                //normalize the 2d vector
            currentSwipe.Normalize();
            if(Mathf.Abs(currentSwipe.y) < 0.1f && Mathf.Abs(currentSwipe.x) < 0.1f)
            {
                if (touch.position.x > (Screen.width / 2))
                {
                    textbox.text= "jump right";
                }
                else if (touch.position.x < (Screen.width / 2))
                {
                    textbox.text= "jump left";

                }
            }
            if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
            {
                textbox.text= "Dash forward";

            }
        }
    }
  }
}