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. Dismiss Notice

Problem with swipe on touch input for iPad

Discussion in 'Scripting' started by hannya93, Feb 22, 2017.

  1. hannya93

    hannya93

    Joined:
    Aug 18, 2016
    Posts:
    6
    Can someone help me on this? I'm trying to make a match-3 game for WebGL that can run on iPad too. It seems I need to manually code the touch input so it can run on iPad. I'm trying to convert this mouse input script into touch input script, but I can't seem to get the swipe method working, while on mouse input, dragging works just fine.

    /// Mouse Input ///
    Code (CSharp):
    1. private void MouseInput () {
    2.         if (state == GameState.None)
    3.         {
    4.             //user has clicked or touched
    5.             if (Input.GetMouseButtonDown (0)) {
    6.                 //get the hit position
    7.                 var hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
    8.                 if (hit.collider != null) { //we have a hit!!!
    9.                     hitGo = hit.collider.gameObject;
    10.                     state = GameState.SelectionStarted;
    11.                 }
    12.             }
    13.         }
    14.         else if (state == GameState.SelectionStarted)
    15.         {
    16.             //user dragged
    17.             if (Input.GetMouseButton (0)) {
    18.                 var hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
    19.                 //we have a hit
    20.                 if (hit.collider != null && hitGo != hit.collider.gameObject) {
    21.                     //user did a hit, no need to show him hints
    22.                     StopCheckForPotentialMatches ();
    23.                     //if the two shapes are diagonally aligned (different row and column), just return
    24.                     if (!Utilities.AreVerticalOrHorizontalNeighbors (hitGo.GetComponent<Shape> (),
    25.                         hit.collider.gameObject.GetComponent<Shape> ())) {
    26.                         state = GameState.None;
    27.                     } else {
    28.                         state = GameState.Animating;
    29.                         FixSortingLayer (hitGo, hit.collider.gameObject);
    30.                         StartCoroutine (FindMatchesAndCollapse (hit));
    31.                     }
    32.                 }
    33.             }
    34.         }
    35.     }
    /// Touch Input ///
    Code (CSharp):
    1. private void TouchInput () {
    2.         if (state == GameState.None)
    3.         {
    4.             //user has clicked or touched
    5.             if (Input.GetTouch(0).phase == TouchPhase.Began) {
    6.                 touchPosWorld = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
    7.                 Vector2 touchPosWorld2D = new Vector2 (touchPosWorld.x, touchPosWorld.y);
    8.                 //get the hit position
    9.                 RaycastHit2D hit = Physics2D.Raycast (touchPosWorld2D, Camera.main.transform.forward);
    10.                 if (hit.collider != null) { //we have a hit!!!
    11.                     hitGo = hit.collider.gameObject;
    12.                     state = GameState.SelectionStarted;
    13.                 }
    14.             }
    15.         }
    16.         else if (state == GameState.SelectionStarted)
    17.         {
    18.             //user dragged
    19.             if (Input.GetTouch(0).phase == TouchPhase.Began) {
    20.                 touchPosWorld = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
    21.                 Vector2 touchPosWorld2D = new Vector2 (touchPosWorld.x, touchPosWorld.y);
    22.                 RaycastHit2D hit = Physics2D.Raycast (touchPosWorld2D, Camera.main.transform.forward);
    23.                 //we have a hit
    24.                 if (hit.collider != null && hitGo != hit.collider.gameObject) {
    25.                     //user did a hit, no need to show him hints
    26.                     StopCheckForPotentialMatches ();
    27.                     //if the two shapes are diagonally aligned (different row and column), just return
    28.                     if (!Utilities.AreVerticalOrHorizontalNeighbors (hitGo.GetComponent<Shape> (),
    29.                         hit.collider.gameObject.GetComponent<Shape> ())) {
    30.                         state = GameState.None;
    31.                     } else {
    32.                         state = GameState.Animating;
    33.                         FixSortingLayer (hitGo, hit.collider.gameObject);
    34.                         StartCoroutine (FindMatchesAndCollapse (hit));
    35.                     }
    36.                 }
    37.             }
    38.         }
    39.     }
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    If you're only using one finger you don't need to use Input.GetTouch etc... - you can just keep using the mouse code and it should work on mobile devices. Also, if you decide to use Input.GetTouch( 0 ) you need to check it's valid first by making sure Input.touchCount is greater than zero.
     
  3. hannya93

    hannya93

    Joined:
    Aug 18, 2016
    Posts:
    6
    problem is, it doesn't work if I only use the mouse code on iPad, since its not an iOS app, but a WebGL game that must be able to be accessed via iPad too.

    and sorry I didn't post the Update method, but the touchCount check is already there too