Skip to content

Instantly share code, notes, and snippets.

@mstfmrt07
Created May 13, 2021 22:18
Show Gist options
  • Save mstfmrt07/ec20d43b9586ad10676ee9e8ff1f034d to your computer and use it in GitHub Desktop.
Save mstfmrt07/ec20d43b9586ad10676ee9e8ff1f034d to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.EventSystems;
public class InputManager : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
public enum Direction { Left, Up, Right, Down, None }
Direction direction;
Vector2 startPos, endPos;
public float swipeThreshold = 100f;
bool draggingStarted;
private void Awake()
{
draggingStarted = false;
direction = Direction.None;
}
public void OnBeginDrag(PointerEventData eventData)
{
draggingStarted = true;
startPos = eventData.pressPosition;
}
public void OnDrag(PointerEventData eventData)
{
if (draggingStarted)
{
endPos = eventData.position;
Vector2 difference = endPos - startPos; // difference vector between start and end positions.
if (difference.magnitude > swipeThreshold)
{
if (Mathf.Abs(difference.x) > Mathf.Abs(difference.y)) // Do horizontal swipe
{
direction = difference.x > 0 ? Direction.Right : Direction.Left; // If greater than zero, then swipe to right.
}
else //Do vertical swipe
{
direction = difference.y > 0 ? Direction.Up : Direction.Down; // If greater than zero, then swipe to up.
}
}
else
{
direction = Direction.None;
}
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (draggingStarted && direction != Direction.None)
{
//Do something with this direction data.
Debug.Log("Swipe direction: " + direction);
}
//reset the variables
startPos = Vector2.zero;
endPos = Vector2.zero;
draggingStarted = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment