How to detect swipe direction in Unity

How to detect swipe direction in Unity

Detect swipe direction in Unity is one of the most common issues to solve. I had this problem in one of my last game: ” Color Slide“. In fact, I had the problem to detect the direction of the swipe in order to move the tail in the right direction.

In this article you’ll see the code along with a very simple technique to detect the swipe direction in Unity, plus, you’ll get a script ready to be used directly in your games.

How to solve the problem?

We want to be able to detect the drag/swipe direction, one common solution, is to see where is the actual position of the mouse/finger (let’s call it position A) on the screen, and on the next frame, see if that position has changed (position B), and if it is, then we can get the vector from the point A to the point B and at this point calculate the direction of the swipe.

The downsides of this approach are:

  1. Write the code to detect the mouse position.
  2. Write the code to detect the finger position in case you’re on mobile.
  3. Write the code get the direction from the point A to point B.
  4. Not very scalable.

Can we do better? Of course, we can :).

How to detect swipe direction in Unity

Believe it or not, Unity has an EventSystem with a few callback, one of this, is the OnEndDrag event. This event is called by a BaseInputModule when a drag is ended.

Isn’t this amazing? We don’t’ have to write the code to detect the drag, plus this method will work with both the mouse and the finger in case you’re on mobile.

Let’s analyze this callback.

OnEndDrag event unity

Method declaration: OnEndDrag(EventSystems.PointerEventData eventData);

This event returns a parameter called eventData which has all the information that we need in order to detect the swipe direction.

Here how we can use this method:

public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("Press position + " + eventData.pressPosition);
Debug.Log("End position + " + eventData.position);
Vector3 dragVectorDirection = (eventData.position - eventData.pressPosition).normalized;
Debug.Log("norm + " + dragVectorDirection);
GetDragDirection(dragVectorDirection);
}

The Vector dragVectorDirection is the vector normalized which will give use the drag direction. To make the code more readable, I create a method GetDragDirection that takes the vector normalized and return and enumerator which identifies the drag direction.

Here is the implementation code

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;
}

Here it is, simple and easy.

Consideration and collusion.

I’ve created a class with all the implementation, ready to be used.

You can Download it by clicking on the button.

If you take a look at the class, you’ll see that besides the implementation of the OnEndDrag event, you also have the empty implementation of the OnDrag event. Why is that? Simply because at the moment that I wrote this tutorial (Unity 5.6) the only way to have the OnEndDrag event called is to implement the OnDrag event otherwise it won’t work.

Hope this article helped you, not it’s me asking your help, please help me grow this community bu sharing this article and don’t forget to follow us in our facebook community.

Written By
More from Marco

Design games around your audience: The four types of player

Design games around your audience: We are used to thinking that all...
Read More

Leave a Reply

Your email address will not be published. Required fields are marked *