Search Unity

Detecting Single Tap (Android)

Discussion in 'Scripting' started by KodaL, May 31, 2011.

  1. KodaL

    KodaL

    Joined:
    Apr 4, 2008
    Posts:
    37
    I have a script to detect a player touching an object and dragging their finger in a direction, in order to move the object in that direction. The problem is the script will also move the object if the player just taps on the object without moving their finger at all.

    I tried to change the various "0's" to 10/-10 in the TouchPhase.Ended if statements. I thought that would make sure the players finger actually moved... but that didn't work out correctly either.

    I actually want the "single tap" to be able to do it's own thing also.

    Code (csharp):
    1. function Update()
    2. {
    3.     if ( Input.touchCount > 0 )
    4.     {
    5.         // --------- raycasting ----------
    6.         var hit : RaycastHit;
    7.         var ray : Ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);    // get finger touch position
    8.         if ( rayCastHitObject )
    9.         {
    10.             if (Input.GetTouch(0).phase == TouchPhase.Moved)
    11.             {
    12.                 touch2H = Input.GetTouch(0).position.x;
    13.                 touch2V = Input.GetTouch(0).position.y;
    14.             }
    15.             if ( Input.GetTouch(0).phase == TouchPhase.Ended)
    16.             {
    17.                 if (touch1H-touch2H > 0  (Mathf.Abs(touch1H-touch2H)) > (Mathf.Abs(touch1V-touch2V)) )
    18.                 {
    19.                     print("Left");
    20.                     // moving code
    21.                 }
    22.                 else if (touch1H-touch2H < 0  (Mathf.Abs(touch1H-touch2H)) > (Mathf.Abs(touch1V-touch2V)) )
    23.                 {
    24.                     print("Right");
    25.                     // moving code
    26.                 }
    27.                 else if (touch1V-touch2V < 0  (Mathf.Abs(touch1H-touch2H)) < (Mathf.Abs(touch1V-touch2V)) )
    28.                 {
    29.                     print("Up");
    30.                     // moving code
    31.                 }
    32.                 else if (touch1V-touch2V > 0  (Mathf.Abs(touch1H-touch2H)) < (Mathf.Abs(touch1V-touch2V)) )
    33.                 {
    34.                     print("Down");
    35.                     // moving code
    36.                 }
    37.                 if (moving != -1)
    38.                 {
    39.                     // all my moving code is here
    40.                     print ("Moving object")
    41.                     rayCastHitObject = false;
    42.                 }
    43.                 touch2H = Input.GetTouch(0).position.x;
    44.                 touch2V = Input.GetTouch(0).position.y;
    45.             }
    46.         }
    47.         else if (Physics.Raycast(ray, hit, rayDistance))
    48.         {
    49.             if (hit.transform.tag == tagName)
    50.             {
    51.                 raycastObject = hit.transform;    
    52.                 touch1H = Input.GetTouch(0).position.x;
    53.                 touch1V = Input.GetTouch(0).position.y;
    54.                 rayCastHitObject = true;
    55.             }
    56.         }
    57.     }
    58. }