Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem rotating object with Touch input

Discussion in 'Scripting' started by amineters, Oct 19, 2019.

  1. amineters

    amineters

    Joined:
    Oct 7, 2019
    Posts:
    1
    Hi,

    I'm trying to rotate a cube with "swipe right and left" movements, so I'm using touch input.

    However, the problem right now is that if I swipe right or left, and I keep my finger touching the screen on the same position, the cube doesn't stop its rotation, it keeps rotating.

    I watched a tutorial to learn how to use Touch Input, so for now my code looks like this.

    P.S: what I want, is that the cube rotates with an amount each time I swipe (for example, if I swipe left, it rotates with an angle to the left), and if the finger is not swiping, the cube should stop, not keeps rotating even if the finger is still in the screen.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LevelController : MonoBehaviour
    4. {
    5.    
    6.     public float turnSpeed = 150f;
    7.     private bool tap, swipeLeft, swipeRight, isDraging;
    8.     private Vector2 startTouch, swipeDelta;
    9.  
    10.     public Vector2 SwipeDelta {get {return swipeDelta;}}
    11.     public bool SwipeLeft {get {return swipeLeft;}}
    12.     public bool SwipeRight {get {return swipeRight;}}
    13.  
    14.     private void Update()
    15.     {
    16.         tap = swipeLeft = swipeRight = false;
    17.         if (Input.GetMouseButtonDown(0))
    18.         {
    19.             isDraging = true;
    20.             tap = true;
    21.             startTouch = Input.mousePosition;
    22.         }
    23.         else if (Input.GetMouseButtonUp(0))
    24.         {
    25.             isDraging = false;
    26.             Reset();
    27.         }
    28.         if (Input.touches.Length >0)
    29.         {
    30.             if (Input.touches[0].phase == TouchPhase.Began)
    31.             {
    32.                 isDraging = true;
    33.                 tap = true;
    34.                 startTouch = Input.touches[0].position;
    35.             }
    36.             else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    37.             {
    38.                 isDraging = false;
    39.                 Reset();
    40.             }
    41.         }
    42.         swipeDelta = Vector2.zero;
    43.         if (isDraging)
    44.         {
    45.             if (Input.touches.Length >0)
    46.             {
    47.                 swipeDelta = Input.touches[0].position - startTouch;
    48.             }
    49.             else if (Input.GetMouseButton(0))
    50.             {
    51.                 swipeDelta = (Vector2)Input.mousePosition - startTouch;
    52.             }
    53.         }
    54.  
    55.         float x = swipeDelta.x;
    56.         if (x > 0)
    57.         {
    58.             swipeRight = true;
    59.         }
    60.         else
    61.         {
    62.             swipeLeft = true;
    63.         }
    64.        
    65.         x = Mathf.Clamp(x, -90, 90);
    66.  
    67.         transform.Rotate(Vector3.forward, x* 1/6 * Time.deltaTime);
    68.  
    69.         if (Input.GetKey(KeyCode.RightArrow))
    70.         {
    71.             transform.Rotate(Vector3.forward, -turnSpeed * Time.deltaTime);
    72.         }
    73.         if (Input.GetKey(KeyCode.LeftArrow))
    74.         {
    75.             transform.Rotate(Vector3.forward, turnSpeed * Time.deltaTime);
    76.            
    77.         }
    78.  
    79.     }
    80.    
    81.     private void Reset()
    82.     {
    83.         startTouch = swipeDelta = Vector2.zero;
    84.         isDraging = false;
    85.     }
    86. }
    87.