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

Question Touch Input system not working on my project

Discussion in 'Input System' started by roaccat, Aug 26, 2023.

  1. roaccat

    roaccat

    Joined:
    Apr 12, 2022
    Posts:
    2
    Hello i'm trying to make my own tetris game on my android phone. I am trying to do,
    1- Touch and release screen making rotation,
    2- Touching and swiping for down, left and right not making any rotation.

    Here is my code, any suggestion would be great, thank you so much!

    Code (CSharp):
    1.     private Vector2 touchStartPos;
    2.     private bool isRotating = false;
    3.     private float swipeThreshold = 75.0f;
    4.  
    5.     private void Update()
    6.     {
    7.         this.board.Clear(this);
    8.  
    9.         this.lockTime += Time.deltaTime;
    10.  
    11.         if (Input.touchCount > 0)
    12.         {
    13.             Touch touch = Input.GetTouch(0);
    14.  
    15.             if (touch.phase == TouchPhase.Began)
    16.             {
    17.                 touchStartPos = touch.position;
    18.                 isRotating = false;
    19.             }
    20.             else if (touch.phase == TouchPhase.Moved)
    21.             {
    22.                 if (!isRotating)
    23.                 {
    24.                     float swipeDistance = touch.position.x - touchStartPos.x;
    25.                     float verticalDistance = touch.position.y - touchStartPos.y;
    26.  
    27.                     if (Mathf.Abs(swipeDistance) > Mathf.Abs(verticalDistance))
    28.                     {
    29.                         if (Mathf.Abs(swipeDistance) > swipeThreshold)
    30.                         {
    31.                             if (swipeDistance > 0)
    32.                             {
    33.                                 Debug.Log("Movement for right.");
    34.                                 Move(Vector2Int.right);
    35.                             }
    36.                             else
    37.                             {
    38.                                 Debug.Log("Movement for left.");
    39.                                 Move(Vector2Int.left);
    40.                             }
    41.                         }
    42.                     }
    43.                     else if (verticalDistance < -swipeThreshold)
    44.                     {
    45.                         Debug.Log("Movement for down.");
    46.                         Move(Vector2Int.down);                      
    47.                     }
    48.                 }
    49.             }
    50.             else if (touch.phase == TouchPhase.Ended)
    51.             {
    52.                 if (!isRotating)
    53.                 {
    54.                     isRotating = true;
    55.                     Debug.Log("Touch detected, processing.");
    56.                    
    57.                     Rotate(1);
    58.                 }
    59.             }
    60.         }
    61.     }
    My all code is attachment.
     

    Attached Files:

  2. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    99
    There is more important problem than input system, which is cognitive complexity of your method (that means there is a lot of nested if else), it makes harder to read and debug your code. I suggest you to fix that first with some techniques (like early return, splitting to local methods, state machine pattern). Then I believe you will find solution yourself. Also there is a very useful plugin if you use Rider https://plugins.jetbrains.com/plugin/12024-cognitivecomplexity/
     
  3. roaccat

    roaccat

    Joined:
    Apr 12, 2022
    Posts:
    2
    Thank you for suggesting but i actually can't find the solution by myself so i still need a help. If i can find the true code i can do the things you said. If you know anything about my solution please share with me. Thank you!

    I check this links too but can't find.
    https://docs.unity3d.com/Manual/MobileInput.html
    https://learn.unity.com/tutorial/touch-input-for-mobile-scripting#
     
  4. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    144
    Funny. I came here for the same thing... and appears... we are working on the same project.
     
  5. faulknordonald

    faulknordonald

    Joined:
    Sep 9, 2019
    Posts:
    144
    I don't know if you figured it out, but I got this:

    Code (CSharp):
    1. board.Clear(this);
    2.  
    3.         if (Input.touchCount > 0)
    4.         {
    5.             theTouch = Input.GetTouch(0);
    6.  
    7.             if (theTouch.phase == TouchPhase.Began)
    8.             {
    9.                 touchStartPosition = theTouch.position;
    10.             }
    11.             else if (theTouch.phase == TouchPhase.Moved || theTouch.phase == TouchPhase.Ended)
    12.             {
    13.                 touchEndPosition = theTouch.position;
    14.  
    15.                 float x = touchEndPosition.x - touchStartPosition.x;
    16.                 float y = touchEndPosition.y - touchStartPosition.y;
    17.  
    18.                 if (Mathf.Abs(x) == 0 && Mathf.Abs(y) == 0)
    19.                 {
    20.                     direction = "Tapped";
    21.                 }
    22.                 else if (Mathf.Abs(x) > Mathf.Abs(y))
    23.                 {
    24.                     direction = x > 0 ? "Right" : "Left";
    25.                     if(direction == "Right")
    26.                     {
    27.                         Move(Vector2Int.right);
    28.                     } else if(direction == "Left")
    29.                     {
    30.                         Move(Vector2Int.left);
    31.                     }
    32.                 }
    33.                 else
    34.                 {
    35.                     direction = y > 0 ? "Up" : "Down";
    36.                     if(direction == "Up")
    37.                     {
    38.                         // Rotate
    39.                         Rotate(1);
    40.                     } else if(direction == "Down")
    41.                     {
    42.                         Move(Vector2Int.down);
    43.                     }
    44.                 }
    45.             }
    46.         }
    47.  
    48.         // End Mobile Input
    49.  
    50.         board.Set(this);
    It's working as the swipes are moving the pieces, the only problem is they're moving way too fast. Still trying to figure that out.