2017-01-05 11 views
0

これはスワイプジェスチャーをシミュレートしようとする私のコードなので、モバイルに構築するとうまくいくことがわかります。何もログに記録されておらず、なぜ動作しないように思われるのか混乱しています。私は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 < -5.0f) && (deltaY >= -1.0f || deltaY <= 1.0f)) 
     { 
      if (startPosition.x < endPosition.x) 
      { 
       print("LTR"); 
      } 
      else 
      { 
       print("RTL"); 
      } 
     } 
     startPosition = endPosition = Vector3.zero; 
    } 
} 
+0

チェックが多すぎます。特に最後のチェック(endPosition!=ゼロ)は真ではありません。 – Everts

答えて

4

私はあなたのコードにいくつかのいくつかの問題を発見することができます。 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"); 
    } 
} 
+0

私はモバイル上でInput.touchesに変更する必要があることを知っていました。コンピューター上でInput.touchesを読み取らないため、コンピューター上でシミュレートしようとしていました。ビルドがモバイルデバイス上にある場合にのみ実行されます。たくさん助けてくれてありがとう。 –

+0

最後の2番目の文は実際には真ではありません。あなたは、あなたのコンピュータに接続されている電話機を、エディタで構築することなくテストすることができます。 [this](http://stackoverflow.com/q/39107153/3785314)を参照し、自分の時間を節約してください。どういたしまして! – Programmer

-1
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"); 
     } 
    } 

} 
} 
関連する問題