Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Input.GetTouch being called every frame, how to stop this?

Discussion in 'Scripting' started by SamuraiKitty, Nov 12, 2015.

  1. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16
    I'm working on a game like ZigZag, for mobile devices. What I want to do is if there is a touch on the right side of the screen, then jump, but if on the left side, then change direction (left or forward).

    I have the jump one working fine, using a simple Raycast to check if the player is grounded, but when I touch the screen on the left to change direction, the code to change direction runs through every frame that the left side of the screen is touched, but I only want it to run through once, changing direction once, not once for every frame the finger is on the screen.

    Here is my code:
    Code (CSharp):
    1. //Touch controls
    2.                 if (Input.touchCount > 0)
    3.                 {
    4.                     Touch touch = Input.GetTouch(0);
    5.                     if (touch.position.x < Screen.width / 2)
    6.                     {
    7.                         if (dir == Vector3.forward)
    8.                         {
    9.                             MoveLeft();
    10.                         }
    11.                         else if (dir == Vector3.left)
    12.                         {
    13.                             MoveRight();
    14.                         }
    15.                         else if (dir == Vector3.zero)
    16.                         {
    17.                             StartMoving();
    18.                         }
    19.                     }
    20.                     else if (touch.position.x > Screen.width / 2)
    21.                     {
    22.                         if (Physics.Raycast(downRay, out hit, rayHeight) && canJump)
    23.                         {
    24.                             Jump();
    25.                         }
    26.                     }
    27.                 }
    I know this is because the code has been placed in the Update() function, but I don't know how I can run through the code only when the left side of the screen is pressed and only once.

    If any more code is required for my question to be answered, please don't hesitate to ask.

    Thanks
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1. if (Input.touchCount > 0) {
    2.    for(int i = 0; i < Input.touchCount; i++) {
    3.       if(Input.GetTouch(i).phase == TouchPhase.Began) {
    4.          Touch touch = Input.GetTouch(i);
    5.          if (touch.position.x < Screen.width / 2) {
    6.             if (dir == Vector3.forward) {
    7.                MoveLeft();
    8.             } else if (dir == Vector3.left) {
    9.                MoveRight();
    10.             } else if (dir == Vector3.zero)  {
    11.                StartMoving();
    12.             }
    13.          } else if (touch.position.x > Screen.width / 2) {
    14.             if (Physics.Raycast(downRay, out hit, rayHeight) && canJump) {
    15.                Jump();
    16.             }
    17.          }
    18.       }
    19.    }
    20. }
    In this way you can also detect multiple touches. If you don't want, just take out for ()
     
  3. SamuraiKitty

    SamuraiKitty

    Joined:
    Jun 29, 2015
    Posts:
    16
    This works, thank you!