using System.Collections; using System.Collections.Generic; using UnityEngine; public class SwipeDetection : Singleton<InputManager> { [SerializeField] private float minimunDistance = .2f; [SerializeField] private float maximunTime = 1f; [SerializeField, Range(0f, 1f)] private float directionThreshold = .9f; [SerializeField] private GameObject trail; private InputManager inputManager; private Vector2 startPosition; private float startTime; private Vector2 endPosition; private float endTime; private Coroutine coroutine; private void Awake() { inputManager = InputManager.Instance; } private void OnEnable() { inputManager.OnStartTouch += SwipeStart; inputManager.OnEndTouch += SwipeEnd; } private void OnDisable() { inputManager.OnStartTouch -= SwipeStart; inputManager.OnEndTouch -= SwipeEnd; } private void SwipeStart(Vector2 position, float time) { startPosition = new Vector3(position.x - 10000, position.y - 10000); startTime = time; trail.SetActive(true); trail.transform.position = position; coroutine = StartCoroutine(Trail()); } private IEnumerator Trail() { while(true) { trail.transform.position = inputManager.PrimaryPosition(); yield return null; } } private void SwipeEnd(Vector2 position, float time) { trail.SetActive(false); StopCoroutine(coroutine); endPosition = position; endTime = time; DetectSwipe(); } private void DetectSwipe() { if (Vector3.Distance(startPosition, endPosition) >= minimunDistance && (endTime - startTime) <= maximunTime) { Debug.Log("Swipe Detected"); Debug.DrawLine(startPosition, endPosition, Color.red, 5f); Vector3 direction = endPosition - startPosition; Vector2 direction2D = new Vector2(direction.x, direction.y).normalized; SwipeDirection(direction2D); } } private void SwipeDirection(Vector2 direction) { if(Vector2.Dot(Vector2.up, direction) > directionThreshold) { Debug.Log("Swipe Up"); } if (Vector2.Dot(Vector2.down, direction) > directionThreshold) { Debug.Log("Swipe down"); } if (Vector2.Dot(Vector2.left, direction) > directionThreshold) { Debug.Log("Swipe left"); } if (Vector2.Dot(Vector2.right, direction) > directionThreshold) { Debug.Log("Swipe right"); } } }