回答
嗨寫了這個腳本未經測試希望它有幫助。
編輯:我沒有發佈這個答案之前搜索,但我認爲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");
}
}
}
}
ty but!我需要檢測 左上和左下/右上和右下/底(左到右)/底(右到左)和頂(右到左)/頂(從左到右) 可能是你看到這個遊戲[鏈接](https://itunes.apple.com/cn/app/fidget-hand-spinner/id1225134960?mt=8) – Abs3akt
@IDontLikeBugs我已經編輯了答案現在它在8個方向工作,如果你想要更多的方向你如果答案有幫助,可以閱讀代碼真正容易 –
@IDontLikeBugs,請通過按答案右側的勾號接受答案 –
- 1. 如何在Unity3d中檢測FB.init()已準備就緒
- 2. iOS 7 uinavigationcontroller如何檢測刷卡?
- 3. Unity3D球沒有被刷卡
- 4. iOS:UIScrollView檢測刷卡手勢
- 5. 如何更改圖像刷卡?
- 6. 如何刷卡圖像+文字的Android
- 7. 檢測刷卡,單擊,按住一個查看Android
- 8. 立體圖像看起來Unity3d
- 9. 如何檢測onTouch或在GoogleMap(V2)上刷卡?
- 10. 如何在閃存中檢測兩個同時刷卡?
- 11. 用刷卡旋轉圖像
- 12. 如何在圖像刷卡視圖上循環?
- 13. unity3d中訪問谷歌玻璃刷卡
- 14. android viewflow從SD卡刷卡圖像
- 15. 蟒蛇魔法檢測圖像就像'二進制'
- 16. android檢測用戶是否被刷卡
- 17. 檢測刷卡速度手機jquery
- 18. iDangerous Swiper沒有檢測到刷卡
- 19. iOS4無法檢測左右刷卡
- 20. 的Android droidQuery刷卡長度檢測
- 21. 與圖像刷卡動作在安卓
- 22. 如何從SD卡檢索圖像
- 23. 在UIScrollView中使用UISwipeGestureRecognizer,無法檢測到右刷卡,只能檢測左刷卡
- 24. 從多個圖像檢測精確的圖像查看
- 25. 如何檢測Android軟鍵盤上的按鍵刷卡
- 26. 如何檢測用戶刷卡的方式?
- 27. 如何通過在Android中刷卡來更改圖像?
- 28. 檢測圖像
- 29. 如何通過視圖刷卡在containerView
- 30. 如何檢測圖像何時失焦?
[檢測輕掃手勢的方向(HTTP的可能重複:// stackoverflow.com/questions/41491765/detect-swipe-gesture-direction) – Isuka
此外,你[已問](http://stackoverflow.com/questions/44049130/how-i-can-detect-swipe-on-unity3d )昨天這個問題,並且因爲同樣的原因而關閉了。嘗試檢查另一個問題,有很多有用的信息讓你開始。 – Isuka
我需要檢測左上和左下/右上和右下/底(左到右)/底部(右到左)和頂部(右到左)/頂部(從左到右)可能會看到這個遊戲[鏈接](https://itunes.apple.com/us/app/fidget-hand-spinner/id1225134960?mt=8) - – Abs3akt