Search Unity

Showcase Swipe Controls for Unity

Discussion in 'Scripting' started by DeemBabbu, Mar 6, 2021.

  1. DeemBabbu

    DeemBabbu

    Joined:
    Jul 18, 2019
    Posts:
    26
    https://github.com/deem7432/Swipe-Controls-Unity/blob/main/InputController.cs


    Here is the Script for controlling players like Subway surfers Type of games.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InputController : MonoBehaviour
    6. {
    7.     public static InputController instance;
    8.  
    9.     private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    10.     private Vector2 startTouch, swipeDelta;
    11.     private bool isDragging = false;
    12.     private float offset = 125f; // DeadZone Offset
    13.  
    14.     public Vector2 SwipeDelta { get { return swipeDelta; } }
    15.     public bool Tap { get { return tap; } }
    16.     public bool SwipLeft { get { return swipeLeft; } }
    17.     public bool SwipeRight { get { return swipeRight; } }
    18.     public bool SwipeUp { get { return swipeUp; } }
    19.     public bool SwipeDown { get { return swipeDown; } }
    20.  
    21.     private void Awake()
    22.     {
    23.         instance = this;
    24.     }
    25.     private void Update()
    26.     {
    27.         tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
    28.  
    29.         #region Standalone
    30.         if (Input.GetMouseButtonDown(0))
    31.         {
    32.             tap = isDragging = true;
    33.             startTouch = Input.mousePosition;
    34.         }
    35.         else if (Input.GetMouseButtonUp(0))
    36.         {
    37.             isDragging = false;
    38.             Reset();
    39.         }
    40.         #endregion
    41.  
    42.         #region Mobile Controls
    43.  
    44.         if (Input.touchCount > 0)
    45.         {
    46.             if (Input.touches[0].phase == TouchPhase.Began)
    47.             {
    48.                 tap = isDragging = true;
    49.                 startTouch = Input.touches[0].position;
    50.             }
    51.             else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    52.             {
    53.                 isDragging = false;
    54.                 Reset();
    55.             }
    56.         }
    57.         #endregion
    58.  
    59.         #region Distance Calculations
    60.         swipeDelta = Vector2.zero;
    61.         if (isDragging)
    62.         {
    63.             if (Input.touchCount > 0)
    64.             {
    65.                 swipeDelta = Input.touches[0].position - startTouch;
    66.             }
    67.             else if (Input.GetMouseButton(0))
    68.             {
    69.                 swipeDelta = (Vector2)Input.mousePosition - startTouch;
    70.             }
    71.         }
    72.         #endregion
    73.  
    74.         #region Direction Define
    75.         if (swipeDelta.magnitude > offset)
    76.         {
    77.             float x = swipeDelta.x;
    78.             float y = swipeDelta.y;
    79.  
    80.             if (Mathf.Abs(x) > Mathf.Abs(y))
    81.             {
    82.                 if (x > 0)
    83.                     swipeRight = true;
    84.                 else
    85.                     swipeLeft = true;
    86.             }
    87.             else
    88.             {
    89.                 if (y > 0)
    90.                     swipeUp = true;
    91.                 else
    92.                     swipeDown = true;
    93.             }
    94.             Reset();
    95.         }
    96.         #endregion
    97.     }
    98.  
    99.     private void Reset()
    100.     {
    101.         swipeDelta = startTouch = Vector2.zero;
    102.         isDragging = false;
    103.     }
    104. }