- Home /
 
 
               Question by 
               samcace · Sep 17, 2017 at 11:13 AM · 
                2d-platformercharacter movementmobile devicesswipe  
              
 
              How can i use single swipe to run a character continuously.
I am making a 2d platformer game and am able to swipe but character only moves one step. I want a single swipe to make character run continuously, another swipe changes direction. here is the section of code responsible for detecting swipe action
   void Update()
 {
     if (Input.touchCount == 1) // user is touching the screen with a single touch
     {
         Touch touch = Input.GetTouch(0); // get the touch
         if (touch.phase == TouchPhase.Began) //check for the first touch
         {
             fp = touch.position;
             lp = touch.position;
         }
         else if (touch.phase == TouchPhase.Moved) // update the last position based on where they moved
         {
             lp = touch.position;
         }
         else if (touch.phase == TouchPhase.Ended) //check if the finger is removed from the screen
         {
             lp = touch.position;  //last touch position. Ommitted if you use list
 
             //Check if drag distance is greater than 20% of the screen height
             if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
             {//It's a drag
              //check if the drag is vertical or horizontal
                 if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                 {   //If the horizontal movement is greater than the vertical movement...
                     if ((lp.x > fp.x))  //If the movement was to the right)
                     {   //Right swipe
                         Debug.Log("Right Swipe");
                         float move = 1f;    //movement in the right direction
                         if (move > 0 && !facingRight){
                             Flip();
                         }
                         Move(move);
                         
                     }
                     else
                     {   //Left swipe
                         Debug.Log("Left Swipe");
                          float move =-1f;   //movement in the left direction
                         if (move < 0 && facingRight){
                             Flip();
                         }
                         Move(move);
                     }
                 }
                 else
                 {   //the vertical movement is greater than the horizontal movement
                     if (lp.y > fp.y)  //If the movement was up
                     {   //Up swipe
                         Debug.Log("Up Swipe");
                     }
                     else
                     {   //Down swipe
                         Debug.Log("Down Swipe");
                     }
                 }
             }
             else
             {   //It's a tap as the drag distance is less than 20% of the screen height
                 Debug.Log("Tap");
                 anim.SetBool("Ground", false);
                 m_Rigidbody2D.AddForce(new Vector2(0f, jumpForce));
             }
         }
     }
 }
 
               And this is the section of code responsible for moving the character
         void Move(float playerDirection)
     {
         anim.SetFloat("speed", Mathf.Abs(playerDirection));        
 
              // Moves Character 
         m_Rigidbody2D.velocity = new Vector2 (playerDirection*maxSpeed, m_Rigidbody2D.velocity.y);
     }
 
               And this is the section of code responsible for changing character direction
 void Flip ()
 {
     facingRight = !facingRight;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }
 
              
               Comment
              
 
               
              Your answer