2017-05-19 23 views
-3

Take a look at this picture如何在Unity3d檢測刷卡就看圖像

我發現如何判斷頂部或底部的聯繫,但我需要更多的細節。我在圖中展示了我需要的東西。告訴我要尋找什麼。

請給我一個提示請

+2

[檢測輕掃手勢的方向(HTTP的可能重複:// stackoverflow.com/questions/41491765/detect-swipe-gesture-direction) – Isuka

+2

此外,你[已問](http://stackoverflow.com/questions/44049130/how-i-can-detect-swipe-on-unity3d )昨天這個問題,並且因爲同樣的原因而關閉了。嘗試檢查另一個問題,有很多有用的信息讓你開始。 – Isuka

+0

我需要檢測左上和左下/右上和右下/底(左到右)/底部(右到左)和頂部(右到左)/頂部(從左到右)可能會看到這個遊戲[鏈接](https://itunes.apple.com/us/app/fidget-hand-spinner/id1225134960?mt=8) - – Abs3akt

回答

1

嗨寫了這個腳本未經測試希望它有幫助。

編輯:我沒有發佈這個答案之前搜索,但我認爲this answer是更好的

公共類GestureRecognizer:MonoBehaviour {

// Min length to detect the Swipe 
public float MinSwipeLength = 5f; 

private Vector2 _firstPressPos; 
private Vector2 _secondPressPos; 
private Vector2 _currentSwipe; 

private Vector2 _firstClickPos; 
private Vector2 _secondClickPos; 
public enum Swipe { 
      None, 
      Up, 
      Down, 
      Left, 
      Right, 
      UpLeft, 
      UpRight, 
      DownLeft, 
      DownRight 
     }; 
public static Swipe SwipeDirection; 
public float ReturnForce = 10f; 

private void Update() { 
    DetectSwipe(); 
} 

public void DetectSwipe() { 
    if (Input.touches.Length > 0) { 
     Touch t = Input.GetTouch(0); 

     if (t.phase == TouchPhase.Began) { 
      _firstPressPos = new Vector2(t.position.x, t.position.y); 
     } 

     if (t.phase == TouchPhase.Ended) { 
      _secondPressPos = new Vector2(t.position.x, t.position.y); 
      _currentSwipe = new Vector3(_secondPressPos.x - _firstPressPos.x, _secondPressPos.y - _firstPressPos.y); 

      // Make sure it was a legit swipe, not a tap 
      if (_currentSwipe.magnitude < MinSwipeLength) { 
       SwipeDirection = Swipe.None; 
       return; 
      } 

      _currentSwipe.Normalize(); 

      // Swipe up 
      if (_currentSwipe.y > 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f) { 
       SwipeDirection = Swipe.Up; 
      } 
       // Swipe down 
      else if (_currentSwipe.y < 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f) { 
       SwipeDirection = Swipe.Down; 
      } 
       // Swipe left 
      else if (_currentSwipe.x < 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f) { 
       SwipeDirection = Swipe.Left; 
      } 
       // Swipe right 
      else if (_currentSwipe.x > 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f) { 
       SwipeDirection = Swipe.Right; 
      } 
       // Swipe up left 
      else if (_currentSwipe.y > 0 && _currentSwipe.x < 0) { 
       SwipeDirection = Swipe.UpLeft; 
      } 
       // Swipe up right 
      else if (_currentSwipe.y > 0 && _currentSwipe.x > 0) { 
       SwipeDirection = Swipe.UpRight; 
      } 
       // Swipe down left 
      else if (_currentSwipe.y < 0 && _currentSwipe.x < 0) { 
       SwipeDirection = Swipe.DownLeft; 

       // Swipe down right 
      } else if (_currentSwipe.y < 0 && _currentSwipe.x > 0) { 
       SwipeDirection = Swipe.DownRight; 
      } 
     } 
    } else { 
     if (Input.GetMouseButtonDown(0)) { 
      _firstClickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y); 
     } else { 
      SwipeDirection = Swipe.None; 
      //Debug.Log ("None"); 
     } 
     if (Input.GetMouseButtonUp(0)) { 
      _secondClickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y); 
      _currentSwipe = new Vector3(_secondClickPos.x - _firstClickPos.x, _secondClickPos.y - _firstClickPos.y); 

      // Make sure it was a legit swipe, not a tap 
      if (_currentSwipe.magnitude < MinSwipeLength) { 
       SwipeDirection = Swipe.None; 
       return; 
      } 

      _currentSwipe.Normalize(); 

      //Swipe directional check 
      // Swipe up 
      if (_currentSwipe.y > 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f) { 
       SwipeDirection = Swipe.Up; 
       Debug.Log("Up"); 
      } 
       // Swipe down 
      else if (_currentSwipe.y < 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f) { 
       SwipeDirection = Swipe.Down; 
       Debug.Log("Down"); 
      } 
       // Swipe left 
      else if (_currentSwipe.x < 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f) { 
       SwipeDirection = Swipe.Left; 
       Debug.Log("Left"); 
      } 
       // Swipe right 
      else if (_currentSwipe.x > 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f) { 
       SwipeDirection = Swipe.Right; 
       Debug.Log("right"); 
      }  // Swipe up left 
      else if (_currentSwipe.y > 0 && _currentSwipe.x < 0) { 
       SwipeDirection = Swipe.UpLeft; 
       Debug.Log("UpLeft"); 

      } 
       // Swipe up right 
      else if (_currentSwipe.y > 0 && _currentSwipe.x > 0) { 
       SwipeDirection = Swipe.UpRight; 
       Debug.Log("UpRight"); 

      } 
       // Swipe down left 
      else if (_currentSwipe.y < 0 && _currentSwipe.x < 0) { 
       SwipeDirection = Swipe.DownLeft; 
       Debug.Log("DownLeft"); 
       // Swipe down right 
      } else if (_currentSwipe.y < 0 && _currentSwipe.x > 0) { 
       SwipeDirection = Swipe.DownRight; 
       Debug.Log("DownRight"); 
      } 
     } 
    } 
} 
+0

ty but!我需要檢測 左上和左下/右上和右下/底(左到右)/底(右到左)和頂(右到左)/頂(從左到右) 可能是你看到這個遊戲[鏈接](https://itunes.apple.com/cn/app/fidget-hand-spinner/id1225134960?mt=8) – Abs3akt

+0

@IDontLikeBugs我已經編輯了答案現在它在8個方向工作,如果你想要更多的方向你如果答案有幫助,可以閱讀代碼真正容易 –

+0

@IDontLikeBugs,請通過按答案右側的勾號接受答案 –