2
\$\begingroup\$

In my script below I'm using horizontal and vertical swipe gesture to move the character but both gestures are called on a single touch.

void Update ()
{
  HorizontalSwipe();
  VerticalSwipe();
}

void HorizontalSwipe()
{
  foreach ( Touch FingerTouchx in Input.touches ) {
    if( FingerTouchx.fingerId < 1 ){  //         FINGERID
      if(FingerTouchx.phase == TouchPhase.Began) {
        FingerInitialPositionx = FingerTouchx.position.x;
      }
      else if(FingerTouchx.phase == TouchPhase.Moved) {
        FingerMovedPositionx = FingerTouchx.position.x;

        if(FingerMovedPositionx > FingerInitialPositionx)
          charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
        else if(FingerMovedPositionx < FingerInitialPositionx)
          charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);
      }
      else if(FingerTouchx.phase == TouchPhase.Ended) {
        FingerInitialPositionx = 0f;
        FingerMovedPositionx = 0f;
      }
      else {
        FingerMovedPositionx = FingerTouchx.position.x;

        if(FingerMovedPositionx > FingerInitialPositionx)
          charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
        else if(FingerMovedPositionx < FingerInitialPositionx)
          charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);
      }
    }
  }
}

void VerticalSwipe()
{
  foreach (Touch FingerTouchy in Input.touches) {
    if( FingerTouchy.fingerId < 1){      //         FINGERID
      if(FingerTouchy.phase == TouchPhase.Began)
        FingerInitialPositiony = FingerTouchy.position.y;

      if(FingerTouchy.phase == TouchPhase.Moved) {
        FingerMovedPositiony = FingerTouchy.position.y;

        if(FingerMovedPositiony > FingerInitialPositiony)
          charcter.transform.Translate(Vector2.up * speed * Time.deltaTime);

        if(FingerMovedPositiony < FingerInitialPositiony)
          charcter.transform.Translate(-Vector2.down * speed * Time.deltaTime);
      }

      if(FingerTouchy.phase == TouchPhase.Ended) {
        FingerInitialPositiony = 0f;
        FingerMovedPositiony = 0f;
      }
    }
  }
}

Whats wrong here? My game character moves in the swipe direction along with continuous jumps.

What I want to achieve is that the character should move horizontally with one touch gesture and vertically(jump) with another touch gesture.

For example: the gesture made on the lower left corner of the touch screen should only consider horizontal movement and make the character move left or right, while the gesture made on the lower right corner should consider only the vertical movement and make the character jump. I'd like to achieve something similar to what's in Leo's Fortune.

\$\endgroup\$
  • \$\begingroup\$ What is happening right now? Can you explain? \$\endgroup\$ – Hamza Hasan Dec 15 '15 at 14:34
  • \$\begingroup\$ My game character moves in the swipe direction along with continuos jumps. \$\endgroup\$ – user75648 Dec 15 '15 at 14:36
  • \$\begingroup\$ And... you want it to go in only one direction per swipe? \$\endgroup\$ – Vaillancourt Dec 15 '15 at 14:43
  • \$\begingroup\$ I found that FingerInitialPositionx&y both are same thats why its acting weird but how do i manage it? \$\endgroup\$ – user75648 Dec 15 '15 at 14:46
  • 1
    \$\begingroup\$ @user75648 Do you want to detect individual swipes, one at a time (they swipe right and lift their finger, you handle one right swipe, they swipe up and lift, you handle one up swipe) or continuous motion during the swipe (while they're dragging to the right, you continuously move to the right)? \$\endgroup\$ – Foggzie Dec 15 '15 at 15:04
3
\$\begingroup\$

The biggest issue is that you're attempting to detect both horizontal and vertical swipes in separate functions but at the same time. When one happens, the other might also happen (unless you swipe at a perfect angle).

Swipe detection is a problem that's been solved before although the link you posted in the comments makes it look like vertical swipes should be individual swipes and left/right is more of a swipe and hold sort of deal.

You'll want to first combine the logic for the swipe, figure out what direction it is, and then handle it appropriately. Here's a modified version of a basic swipe detection script:

public class SwipeHandler : MonoBehaviour {
    private float m_startTime  = 0.0f;
    private Vector2 m_startPos = Vector2.zero;

    private bool m_swiping = false;
    private bool m_holding = false;
    private float m_minDist  = 50.0f;
    private float m_maxTime = 0.5f;

    void Update () {
        if (Input.touchCount > 0){
            foreach (Touch touch in Input.touches) {
                switch (touch.phase) {
                case TouchPhase.Began :
                    m_swiping = true;
                    m_startTime = Time.time;
                    m_startPos = touch.position;
                    break;
                case TouchPhase.Canceled :
                    m_holding = false;
                    m_swiping = false;
                    break;
                case TouchPhase.Ended :
                    DetectSwipe(touch);
                    m_holding = false;
                    break;
                case TouchPhase.Moved :
                    DetectSwipe(touch);
                    break;
                case TouchPhase.Stationary :
                    DetectSwipe(touch);
                    break;
                }
            }
        }
    }

    void DetectSwipe (Touch touch) {
        float totalTime = Time.time - m_startTime;
        float totalDist = (touch.position - m_startPos).magnitude;
        if (m_holding || (m_swiping && totalTime < m_maxTime && totalDist > m_minDist)) {
            Vector2 direction = touch.position - m_startPos;
            Vector2 swipeType = Vector2.zero;
            if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)) {
                // Horizontal:
                swipeType = Vector2.right * Mathf.Sign(direction.x);
            }
            else{
                // Vertical:
                swipeType = Vector2.up * Mathf.Sign(direction.y);
            }

            if(swipeType.x != 0.0f){
                 m_holding = true;
                 if(swipeType.x > 0.0f){
                     // HANDLE RIGHT SWIPE
                 }
                 else{
                     // HANDLE LEFT SWIPE
                 }
            }
            if(swipeType.y != 0.0f ){
                 m_swiping = false;  // <- THIS MAKES THE DIFFERENCE
                 if(swipeType.y > 0.0f){
                     // HANDLE UP SWIPE
                 }
                 else{
                     // HANDLE DOWN SWIPE
                 }
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ how to divide the touch screen to half so that i can use horizontal gesture on left side and vertical gesture on other half side in the code? \$\endgroup\$ – user75648 Dec 15 '15 at 16:46
  • \$\begingroup\$ @user75648 Check the x component of the starting position of the drag against Screen.width / 2 and you can determine that pretty easily. \$\endgroup\$ – Foggzie Dec 15 '15 at 16:53
  • \$\begingroup\$ Tested on iPhone but the character jumps to its left side when swiped up(with still holding the horizontal swipe) don't know whats wrong. \$\endgroup\$ – user75648 Dec 17 '15 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.