Last active
June 17, 2024 10:08
-
-
Save Zidan-kohai/350d60a43b4dda8e7cea7a1a7e5fbcaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
public class SwipeDetector : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler | |
{ | |
private Vector2 lastPosition; | |
public Vector2 swipeDelta; | |
private Coroutine coroutine; | |
public void OnBeginDrag(PointerEventData data) | |
{ | |
lastPosition = data.position; | |
} | |
public void OnDrag(PointerEventData data) | |
{ | |
swipeDelta = data.position - lastPosition; | |
if (swipeDelta.magnitude < 2) | |
{ | |
swipeDelta = Vector2.zero; | |
} | |
else | |
{ | |
swipeDelta.Normalize(); | |
} | |
lastPosition = data.position; | |
if(coroutine != null) StopCoroutine(coroutine); | |
coroutine = StartCoroutine(Wait()); | |
} | |
public void OnEndDrag(PointerEventData eventData) | |
{ | |
swipeDelta = Vector2.zero; | |
} | |
private IEnumerator Wait() | |
{ | |
yield return new WaitForSeconds(0.01f); | |
swipeDelta = Vector2.zero; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment