Search Unity

Mobile Taping Issue

Discussion in 'Scripting' started by Frostbite23, May 27, 2015.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Hey everyone, I'm build a navigation app and currently right now I have a problem on my Map screen.

    There are 4 sprites in the scene, these sprites take up the mass majority of the scene. So to best explain my problem through text..

    You know how on your phone, you to a view your contacts and you scroll through many contacts in there. But if you noticed, the buttons arrent being activated. Or if you sometimes hold your finger over some place and drag it over a button, the button will not be activated until you let go and then tap the button. In This case it is exactly the opposite.

    I believe its called ghost touching or something like that, When I'm moving and my finger is over a button, the button thinks I'm click when really I'm not.

    Here are my scripts.
    My mobile movement
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var zoomSpeed : float = 0.5f;
    4. var maxZoom : float = 0.0f;
    5. var minZoom : float = 0.0f;
    6.  
    7. private var fingerPos : Vector2 = Vector2.zero;
    8. private var lastPos : Vector2 = Vector2.zero;
    9. private var movedBy : Vector2 = Vector2.zero;
    10.  
    11. private var xPos : float = 0.0f;
    12. private var yPos : float = 0.0f;
    13.  
    14. var sensitivity : float = 3.0f;
    15.  
    16. var maximumX : float = 0.0f;
    17. var minumumX : float = 0.0f;
    18.  
    19. var maximumY : float = 0.0f;
    20. var minumumY : float = 0.0f;
    21.  
    22. private var zoom = false;
    23.  
    24. private var foto : Camera;
    25. var usesIndicator = false;
    26. var identifer : GameObject;
    27. var indicator : GameObject;
    28. var tap : float = 0.0f;
    29.  
    30. function Awake () {
    31.     foto = GetComponent(Camera);
    32.     if(usesIndicator){
    33.         identifer.SetActive(false);
    34.         indicator.SetActive(false);
    35.     }
    36.      transform.position = Vector3(Mathf.Clamp(transform.position.x, maximumX, minumumX), Mathf.Clamp(transform.position.y, maximumY, minumumY), transform.position.z);//clamp x
    37. }
    38.  
    39. function LateUpdate () {
    40.     if (Input.touchCount == 1){
    41.         var touch : Touch = Input.GetTouch(0);
    42.         if(touch.phase == TouchPhase.Began){
    43.             fingerPos = Vector2.zero;
    44.             lastPos = Vector2.zero;
    45.             movedBy = Vector2.zero;
    46.          
    47.             xPos = 0;
    48.             yPos = 0;
    49.             tap = 1;
    50.          
    51.             fingerPos = touch.position;
    52.             zoom = true;
    53.             if(usesIndicator){
    54.                 identifer.SetActive(true);
    55.                 indicator.SetActive(true);
    56.             }
    57.         }
    58.      
    59.         else if(touch.phase == TouchPhase.Moved){
    60.             movedBy = touch.position - fingerPos;
    61.             lastPos = fingerPos;
    62.             fingerPos = touch.position;
    63.          
    64.             xPos = movedBy.x / Screen.width;
    65.             yPos = movedBy.y / Screen.width;
    66.             zoom = false;
    67.             tap = 1;
    68.         }
    69.         else if(touch.phase == TouchPhase.Stationary){
    70.             lastPos = fingerPos;
    71.             fingerPos = touch.position;
    72.          
    73.             xPos = 0;
    74.             yPos = 0;
    75.             zoom = true;
    76.             tap = 1;
    77.         }
    78.         else if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){
    79.             xPos = 0;
    80.             yPos = 0;
    81.             zoom = true;
    82.             tap = 0;
    83.         }
    84.     }
    85.  
    86.     transform.position += Vector3(-xPos * sensitivity, -yPos * sensitivity, 0.0f);
    87.  
    88.     if(Input.touchCount == 2 && zoom == true){
    89.         var fingerOne = Input.GetTouch(0);
    90.         var fingerTwo = Input.GetTouch(1);
    91.      
    92.         var lastOnePos = fingerOne.position - fingerOne.deltaPosition;
    93.         var lastTwoPos = fingerTwo.position - fingerTwo.deltaPosition;
    94.      
    95.         var lastMag = (lastOnePos - lastTwoPos).magnitude;
    96.         var deltaMag = (fingerOne.position - fingerTwo.position).magnitude;
    97.      
    98.         var magDiff = lastMag - deltaMag;
    99.      
    100.         foto.orthographicSize += magDiff * zoomSpeed;
    101.         foto.orthographicSize = Mathf.Clamp(foto.orthographicSize, minZoom, maxZoom);
    102.     }
    103. }
    My buttons script
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var cam : Camera;
    4. var selectedCol : Color;
    5. var camZoom : float = 0.0f;
    6. var level : String;
    7. var mobilemove : MobileMovement;
    8.  
    9. var loadingShit : GameObject;
    10. private var zoom = false;
    11. private var defaultCol : Color;
    12. private var spriteRender : SpriteRenderer;
    13.  
    14. function Awake () {
    15.     spriteRender = gameObject.GetComponent.<SpriteRenderer>();
    16.     defaultCol = spriteRender.color;
    17. }
    18.  
    19. function FixedUpdate () {
    20.     if(zoom){
    21.         ZoomAnim();
    22.     }
    23.     OnTouch();
    24. }
    25.  
    26. function OnMouseDown () {
    27. /*    spriteRender.color = selectedCol;
    28.     yield WaitForSeconds (0.1f);
    29.     spriteRender.color = defaultCol;
    30.     zoom = true;*/
    31. }
    32.  
    33. function OnTouch () {
    34.     if(Input.touchCount > 0) {
    35.         var touch : Touch = Input.GetTouch(0);
    36.         if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){
    37.             spriteRender.color = selectedCol;
    38.             yield WaitForSeconds (0.1f);
    39.             spriteRender.color = defaultCol;
    40.             zoom = true;
    41.         }
    42.     }
    43. }
    44.  
    45. function ZoomAnim () {
    46.     cam.transform.position = Vector3( Mathf.Lerp(cam.transform.position.x, gameObject.transform.position.x, Time.deltaTime * 5.0),
    47.                                         Mathf.Lerp(cam.transform.position.y, gameObject.transform.position.y, Time.deltaTime * 5.0), cam.transform.position.z);
    48.     cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, camZoom, Time.deltaTime * 5.0);
    49.     loadingShit.SetActive(true);
    50.     yield WaitForSeconds(1);
    51.     Loading();
    52. }
    53.  
    54. function Loading () {
    55.     var async : AsyncOperation = Application.LoadLevelAsync(level);
    56.     yield async;
    57. }
    Anyone had this issue before that has a solution for my problem? Please?

    P.S excuse my lousy coding.
     
    Last edited: May 28, 2015
  2. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    You want to register a click only when the user removes the finger, not at the moment the user touches the screen.

    You also might want to check the duration from the user presses until he releases, and the difference in position.
     
  5. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    I thought about that but yeah that would require some timing, trouble is i don't how to put it to code.
     
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    You could probably use the Untiy Event System for this. There are also gesture packages on the asset store that will handle it for you.