Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Problem with swipecontrols script

Discussion in 'Scripting' started by Jiry_XD, Aug 20, 2020.

  1. Jiry_XD

    Jiry_XD

    Joined:
    Aug 3, 2020
    Posts:
    20
    Hey people, I used this tutorial to add swipecontrols to my game, but it is not working, can anyone help? I use these 2 scripts. I've been trying to fix this the past day but it isn't working can anyone please help me :)

    Thanks in advance :)
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Movement : MonoBehaviour
    4. {
    5.     public bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    6.     public Vector2 startTouch, swipeDelta;
    7.     public bool isdragging;
    8.     private void Update()
    9.     {
    10.         tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
    11.  
    12.        
    13.         //Standalone Inputs
    14.         if (Input.GetMouseButtonDown(0))
    15.         {
    16.             tap = true;
    17.             isdragging = true;
    18.             startTouch = Input.mousePosition;
    19.         }
    20.         else if (Input.GetMouseButtonUp(0))
    21.         {
    22.             isdragging = false;
    23.             Reset();
    24.         }
    25.         //Mobile inputs
    26.         if(Input.touches.Length > 0)
    27.         {
    28.             if (Input.touches[0].phase == TouchPhase.Began)
    29.             {
    30.                 tap = true;
    31.                 isdragging = true;
    32.                 startTouch = Input.touches[0].position;
    33.             }
    34.             else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    35.             {
    36.                 isdragging = false;
    37.                 Reset();
    38.             }
    39.  
    40.             //Calculate the distance
    41.             swipeDelta = Vector2.zero;
    42.             if(isdragging)
    43.             {
    44.                 if(Input.touches.Length > 0)
    45.                 swipeDelta = Input.touches[0].position - startTouch;
    46.                     else if (Input.GetMouseButton(0))
    47.                     swipeDelta = (Vector2)Input.mousePosition - startTouch;
    48.                    
    49.                 // Did we cross the deadzone?
    50.                 if(swipeDelta.magnitude > 125)
    51.                 {
    52.                     float x = swipeDelta.x;
    53.                     float y = swipeDelta.y;
    54.                     if(Mathf.Abs(x) > Mathf.Abs(y))
    55.                     {
    56.                         if (x < 0)
    57.                             swipeLeft = true;
    58.                         else swipeRight = true;
    59.                     }
    60.                     else
    61.                     {
    62.                         if (y < 0)
    63.                             swipeDown = true;
    64.                         else
    65.                             swipeUp = true;
    66.  
    67.  
    68.  
    69.  
    70.  
    71.  
    72.                     }
    73.                            
    74.                    
    75.  
    76.                    
    77.                     Reset();
    78.                 }
    79.             }
    80.         }
    81.  
    82.  
    83.  
    84.     }
    85.  
    86.     private void Reset()
    87.     {
    88.         startTouch = swipeDelta = Vector2.zero;
    89.         isdragging = false;
    90.     }
    91.  
    92.  
    93.  
    94. }
    95.  
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class swipeMovement : MonoBehaviour
    4. {
    5.     public Movement swipecontrols;
    6.     public Transform player;
    7.     private Vector3 desiredPosition;
    8.     public Rigidbody rb;
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if(swipecontrols.swipeLeft)
    14.             rb.AddForce(-2000f * Time.deltaTime, 0, 0);
    15.         if(swipecontrols.swipeRight)
    16.              rb.AddForce(2000f * Time.deltaTime, 0, 0);
    17.         if(swipecontrols.swipeUp)
    18.             rb.AddForce(0, 0, 2000f * Time.deltaTime);
    19.         if(swipecontrols.swipeDown)
    20.              rb.AddForce(0, 0, -2000f * Time.deltaTime);
    21.  
    22.  
    23.         player.transform.position = Vector3.MoveTowards(player.transform.position, desiredPosition, 3f * Time.deltaTime);
    24.  
    25.     }
    26. }
    27.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Can you please elaborate on this?

    Is it not compiling?
    Is there an error at runtime?
    What behavior are you expecting and what behavior are you seeing instead?