Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to implement swipe mechanic while character running constantly

Discussion in 'Getting Started' started by Lochnload, Oct 6, 2021.

  1. Lochnload

    Lochnload

    Joined:
    Mar 2, 2020
    Posts:
    1
    Hello everybody,

    I am pretty new to unity and programming. This is actually my first ever help call from any platform. I am stuck at some point in my progress. Here it comes.

    I successfully implemented swipe mechanic to my player object. Depends on swipe direction, my player moves to that direction with current position + vector3.left,right,up,down unit. Problem is I am trying to make my player run constantly and swipe when needed. However, whenever I swipe while running player moves weirdly. What should be my logic in my code. Here is my code;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Swipe : MonoBehaviour
    6. {
    7.     [HideInInspector]
    8.     public bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    9.     [HideInInspector]
    10.     public Vector2 startTouch, swipeDelta;
    11.     private bool isDragging = false;
    12.  
    13.     private void Update()
    14.     {
    15.        
    16.         tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
    17.      
    18.          #region Inputs
    19.         if (Input.touches.Length != 0)
    20.         {
    21.             if (Input.touches[0].phase == TouchPhase.Began)
    22.             {
    23.                 isDragging = true;
    24.                 tap = true;
    25.                 startTouch = Input.touches[0].position;
    26.             }
    27.             else if(Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    28.             {
    29.                 isDragging = false;
    30.                 Reset();
    31.             }
    32.             #endregion
    33.         }
    34.  
    35.         //calculate Distance
    36.         swipeDelta = Vector2.zero;
    37.         if(isDragging)
    38.         {
    39.             if(Input.touches.Length > 0)
    40.             {
    41.                 swipeDelta = Input.touches[0].position - startTouch;
    42.             }
    43.         }
    44.         //did we cross the deadzone ?
    45.         if(swipeDelta.magnitude > 50)
    46.         {
    47.             // which direction ?
    48.             float x = swipeDelta.x;
    49.             float y = swipeDelta.y;
    50.             if(Mathf.Abs(x) > Mathf.Abs(y))
    51.             {
    52.                 // left or right
    53.                 if (x < 0)
    54.                
    55.                     swipeLeft = true;
    56.                     else
    57.                     swipeRight = true;
    58.                
    59.             }
    60.             else
    61.             {
    62.                 if (y < 0)
    63.  
    64.                     swipeDown = true;
    65.                 else
    66.                     swipeUp = true;
    67.  
    68.             }
    69.             Reset();
    70.         }
    71.  
    72.     }
    73.  
    74.     private void Reset()
    75.     {
    76.         startTouch = swipeDelta = Vector2.zero;
    77.         isDragging = false;
    78.     }
    79. }

    AND

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SwipeTest : MonoBehaviour
    6. {
    7.     private Rigidbody playerRb;
    8.     public Swipe swipeControls;
    9.     public Transform player;
    10.     private Vector3 desiredPosition;
    11.    
    12.  
    13.     private void Start()
    14.     {
    15.         playerRb = GetComponent<Rigidbody>();
    16.         desiredPosition = transform.position;
    17.     }
    18.     // Update is called once per frame
    19.     private void Update()
    20.     {
    21.        
    22.        
    23.  
    24.             if (swipeControls.swipeLeft)
    25.  
    26.                 desiredPosition += Vector3.left;
    27.             if (swipeControls.swipeRight)
    28.                 desiredPosition += Vector3.right;
    29.             if (swipeControls.swipeUp)
    30.                 desiredPosition += Vector3.forward;
    31.             if (swipeControls.swipeDown)
    32.                 desiredPosition += Vector3.back;
    33.  
    34.  
    35.             player.transform.position = Vector3.MoveTowards(player.transform.position, desiredPosition, 3f * Time.deltaTime);
    36.        
    37.        
    38.        
    39.     }
    40. }
    Thanks for your time.