检测滑动手势方向

这是我尝试模拟滑动手势的代码,因此当我构建移动设备时,我知道它会起作用。 什么都没有记录,我很困惑为什么它似乎不起作用。 我希望它在控制台中打印出来,我可以刷RTL (从右到左)或LTR (从左到右)。 我不明白我做错了什么。

 void Update() { if (Input.GetMouseButtonDown(0)) { startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); } if (Input.GetMouseButtonUp(0)) { endPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); } if (startPosition != endPosition && startPosition != Vector3.zero && endPosition != Vector3.zero) { float deltaX = endPosition.x - startPosition.x; float deltaY = endPosition.y - startPosition.y; if ((deltaX > 5.0f || deltaX = -1.0f || deltaY <= 1.0f)) { if (startPosition.x < endPosition.x) { print("LTR"); } else { print("RTL"); } } startPosition = endPosition = Vector3.zero; } } 

我可以在你的代码中发现一些问题。 将Vector3==!=进行比较并不是一个好主意。 近似比较很好。 您正在移动平台上使用Input.GetMouseButtonDown

您需要使用Input.touches来执行此操作。 循环遍历它,将起始位置存储在TouchPhase.Began ,然后将结果位置存储在TouchPhase.Ended 。 然后,您可以使用这两个变量来确定手指的方向。

TouchPhase.Moved的帮助下,即使手指尚未释放,下面的代码也会检测滑动方向。 您可以通过启用detectSwipeOnlyAfterRelease布尔变量来禁用它。 您还可以修改SWIPE_THRESHOLD以获得灵敏度。

 public class SwipeDetector : MonoBehaviour { private Vector2 fingerDown; private Vector2 fingerUp; public bool detectSwipeOnlyAfterRelease = false; public float SWIPE_THRESHOLD = 20f; // Update is called once per frame void Update() { foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began) { fingerUp = touch.position; fingerDown = touch.position; } //Detects Swipe while finger is still moving if (touch.phase == TouchPhase.Moved) { if (!detectSwipeOnlyAfterRelease) { fingerDown = touch.position; checkSwipe(); } } //Detects swipe after finger is released if (touch.phase == TouchPhase.Ended) { fingerDown = touch.position; checkSwipe(); } } } void checkSwipe() { //Check if Vertical swipe if (verticalMove() > SWIPE_THRESHOLD && verticalMove() > horizontalValMove()) { //Debug.Log("Vertical"); if (fingerDown.y - fingerUp.y > 0)//up swipe { OnSwipeUp(); } else if (fingerDown.y - fingerUp.y < 0)//Down swipe { OnSwipeDown(); } fingerUp = fingerDown; } //Check if Horizontal swipe else if (horizontalValMove() > SWIPE_THRESHOLD && horizontalValMove() > verticalMove()) { //Debug.Log("Horizontal"); if (fingerDown.x - fingerUp.x > 0)//Right swipe { OnSwipeRight(); } else if (fingerDown.x - fingerUp.x < 0)//Left swipe { OnSwipeLeft(); } fingerUp = fingerDown; } //No Movement at-all else { //Debug.Log("No Swipe!"); } } float verticalMove() { return Mathf.Abs(fingerDown.y - fingerUp.y); } float horizontalValMove() { return Mathf.Abs(fingerDown.x - fingerUp.x); } //////////////////////////////////CALLBACK FUNCTIONS///////////////////////////// void OnSwipeUp() { Debug.Log("Swipe UP"); } void OnSwipeDown() { Debug.Log("Swipe Down"); } void OnSwipeLeft() { Debug.Log("Swipe Left"); } void OnSwipeRight() { Debug.Log("Swipe Right"); } } 
 Try this Out. I hope this helps. void Update(){ if (Input.GetMouseButtonDown(0)){ startPosition = Input.mousePosition; } if (Input.GetMouseButtonUp(0)){ float swipe = startPosition.x - Input.mousePosition.x; } if (swipe < 0) { print("LTR"); } else{ print("RTL"); } } } }