Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Swipe : MonoBehaviour
- {
- [SerializeField] private GameObject _player;
- private Vector2 _startTouchPosition;
- private Vector2 _currentPosition;
- private bool _stopTouch = false;
- private float _swipeRange = 5;
- private void Update()
- {
- Svipe();
- }
- private void Svipe()
- {
- if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
- {
- _startTouchPosition = Input.GetTouch(0).position;
- }
- if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
- {
- _currentPosition = Input.GetTouch(0).position;
- Vector2 distance = _currentPosition - _startTouchPosition;
- if (!_stopTouch)
- {
- if (distance.x < -_swipeRange)
- {
- _player.transform.position += Vector3Int.left;
- _stopTouch = true;
- }
- if (distance.x > _swipeRange)
- {
- _player.transform.position += Vector3Int.right;
- _stopTouch = true;
- }
- }
- }
- if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
- {
- _stopTouch = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement