Hi guys, I wrote this simple tutorial wich is basically 7 lines of code and works like a charm. Easy and simple. You just have to use te build in Unity event system instead to write long and useless code.
No need to use an update or fixed update.
Here a snipped of the code:
public void OnEndDrag(PointerEventData eventData)
{
Vector3 dragVectorDirection = (eventData.position - eventData.pressPosition).normalized;
GetDragDirection(dragVectorDirection);
}
private enum DraggedDirection
{
Up,
Down,
Right,
Left
}
private DraggedDirection GetDragDirection(Vector3 dragVector)
{
float positiveX = Mathf.Abs(dragVector.x);
float positiveY = Mathf.Abs(dragVector.y);
DraggedDirection draggedDir;
if (positiveX > positiveY)
{
draggedDir = (dragVector.x > 0) ? DraggedDirection.Right : DraggedDirection.Left;
}
else
{
draggedDir = (dragVector.y > 0) ? DraggedDirection.Up : DraggedDirection.Down;
}
Debug.Log(draggedDir);
return draggedDir;
}
As you can see is very easy. Uf you want to know more you can download the full source code in the article.
The main advantages of this system is that It’ll work in both Mobile and non-mobile platforms