-3

Take a look at this picture

I found how to determine the touch on the top or bottom but I need more details. I showed in the picture what I need. Tell me what to look for.

Give me a hint please

Nino
  • 6,931
  • 2
  • 27
  • 42
Abs3akt
  • 387
  • 4
  • 19
  • 2
    Possible duplicate of [Detect swipe gesture direction](http://stackoverflow.com/questions/41491765/detect-swipe-gesture-direction) – Izuka May 19 '17 at 07:19
  • 2
    Also, you [already asked](http://stackoverflow.com/questions/44049130/how-i-can-detect-swipe-on-unity3d) this question yesterday, and it was closed for the same reason. Try to check the other question, there is a lot of useful information to get you started. – Izuka May 19 '17 at 07:20
  • I need detect LeftUP and Left Down / Right Up and Right Down / bottom (Left to Right) / bottom (Right to Left) and Top(Right to left) / Top (Left to Right) may be u see this game [link](https://itunes.apple.com/us/app/fidget-hand-spinner/id1225134960?mt=8) – – Abs3akt May 19 '17 at 07:51
  • 1
    Basically both the answer posted there and the other question can help you to start. You just also need to check the sign of currentSwipe.x or currentSwipe.y to know in which direction you are going. – Izuka May 19 '17 at 07:55

1 Answers1

1

Hi wrote this script untested hope it helps.

Edit : i did not searched before posting this answer but i think this answer is Better

public class 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" );
            }
        }
    }
}
Community
  • 1
  • 1
Syed Anwar Fahim
  • 306
  • 5
  • 15
  • ty but! I need detect LeftUP and Left Down / Right Up and Right Down / bottom (Left to Right) / bottom (Right to Left) and Top(Right to left) / Top (Left to Right) may be u see this game [link](https://itunes.apple.com/us/app/fidget-hand-spinner/id1225134960?mt=8) – Abs3akt May 19 '17 at 07:41
  • @IDontLikeBugs i have edited the answer now it work in 8 directions and if you want more direction you can just read the code its real easy – Syed Anwar Fahim May 19 '17 at 10:19
  • @IDontLikeBugs if the answer was helpful please accept it as an answer by pressing the tick on the right side of answer – Syed Anwar Fahim May 22 '17 at 05:08