Issue with Touch Buttons

Hey everyone, sorry for the generic question but I don’t know how to word this without it sounding confusing. Basically I have a left and right arrow on my screen that move the player when physically held on the phone (touch controls). My problem is that when you click and hold the right arrow button, the player walks right (like they should) but will keep walking even if you drag your finger off of that button. I don’t mean that once you release the button he keeps walking, cause he doesn’t. I mean that if you press and hold the button and slide your finger to anywhere else on the screen (while never taking your finger off the screen) he keeps walking. How do I make it that if the player slides there finger off of the button, the function stops playing? Here is my code:

         if (Input.GetKey(KeyCode.LeftArrow))
         {
             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
             transform.localScale = new Vector3(-1, 1, 1);
 
         }
        if (Input.GetKey(KeyCode.RightArrow))
         {
             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
             transform.localScale = new Vector3(-1, 1, 1);
         }
 

         if (moveright)
         {
             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
             transform.localScale = new Vector3(-1, 1, 1);
          }
         if (moveleft)
         {
             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
             transform.localScale = new Vector3(-1, 1, 1);
         }
 
     }

If this is something that can be done in inspector view or if their is an alternative, let me know. Thanks guys :slight_smile:

You say that tou are using touch controllers, but your code is using the arrow keys.

Seems like you are using the Unity UI, so just add an Event Trigger component an use it to update your functions.