Advertisement
Nemesis_War

swap

Oct 27th, 2021
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Swipe : MonoBehaviour
  7. {
  8.     [SerializeField] private GameObject _player;
  9.     private Vector2 _startTouchPosition;
  10.     private Vector2 _currentPosition;
  11.     private bool _stopTouch = false;
  12.     private float _swipeRange = 5;
  13.  
  14.     private void Update()
  15.     {
  16.         Svipe();
  17.     }
  18.  
  19.     private void Svipe()
  20.     {
  21.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
  22.         {
  23.             _startTouchPosition = Input.GetTouch(0).position;
  24.         }
  25.  
  26.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
  27.         {
  28.             _currentPosition = Input.GetTouch(0).position;
  29.             Vector2 distance = _currentPosition - _startTouchPosition;
  30.  
  31.             if (!_stopTouch)
  32.             {
  33.                 if (distance.x < -_swipeRange)
  34.                 {
  35.                     _player.transform.position += Vector3Int.left;
  36.                     _stopTouch = true;
  37.                 }
  38.                 if (distance.x > _swipeRange)
  39.                 {
  40.                     _player.transform.position += Vector3Int.right;
  41.                     _stopTouch = true;
  42.                 }
  43.             }
  44.         }
  45.  
  46.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
  47.         {
  48.             _stopTouch = false;
  49.         }
  50.  
  51.     }
  52. }
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement