- Home /
 
 
               Question by 
               bobmcdonal · May 02, 2021 at 10:31 PM · 
                2dscripting problemmovement scripttouch controlstap  
              
 
              Tap vs touch controls
I'm creating a movement script where the player must tap either the left or right side of the screen. Currently my script is working however I can simply hold my finger on the screen to move continuously in the respective direction. It shouldn't be allowed to happen. How can I change it so that it only registers a tap and not a touch/hold?
 void Update()
     {
         if (Input.touchCount > 0)
         {
             var touch = Input.GetTouch(0);
             float jumpVelocity = 3f;
             if (touch.position.x < Screen.width / 2)
             {
                 rigidbody2d.velocity = (Vector2.up * jumpVelocity) + (Vector2.left * jumpVelocity);
             }
             else if (touch.position.x > Screen.width / 2)
             {
                 rigidbody2d.velocity = (Vector2.up * jumpVelocity) + (Vector2.right * jumpVelocity);
             }
         }
     }
 
              
               Comment
              
 
               
              Answer by Fariborzzn · May 03, 2021 at 02:10 AM
Hey @bobmcdonal . Every touch on screen has property called TouchPhase
you can know the phase of your touch by read its value . so simply do edit your code to this :
 void Update()
      {
          if (Input.touchCount > 0)
          {
             var touch = Input.GetTouch(0);
             switch (touch.phase)
             {
                          //When a touch has first been detected record the starting position
                 case TouchPhase.Began:
                          // Record initial touch position.
                     var   startPos = touch.position;
                     break;
 
                         //Determine if the touch is a moving touch
                 case TouchPhase.Moved:
                        // you dont need this one 
                     break;
 
                 case TouchPhase.Ended:
                     // Report that the touch has ended when it ends
                    var endpos= touch.position;
                     if(startpos==endpos)
                         {
                            float jumpVelocity = 3f;
                            if (touch.position.x < Screen.width / 2)
                            {
                               rigidbody2d.velocity = (Vector2.up * jumpVelocity) + 
                               (Vector2.left * jumpVelocity);
                            }
                            else if (touch.position.x > Screen.width / 2)
                            {
                             rigidbody2d.velocity = (Vector2.up * jumpVelocity) + 
                             (Vector2.right * jumpVelocity);
                             }
                          }
                     break;
             }
 
          }
      }
 
               best regard Fariborz
Your answer
 
             Follow this Question
Related Questions
Buttons touchscreen movement bug 1 Answer
2D Movement with axis? 1 Answer
Help with Mobile Game controls 2 Answers