Swipe Detection in Unity
Swipes are very popular and useful gestures for controlling our mobile games’ behaviors. It is very vital for our games to seamlessly integrate this mechanic, which is widely used in many genres from endless runner games to puzzle games. In this article, we will handle the swipe controls in Unity game engine.
First of all, let’s create a new project by clicking the ‘New’ button from Unity Hub, indicating the name and path of our project and choosing one of the 2D/3D options. After opening our project, right click into the ‘Assets’ folder and choose ‘Create C# Script’ option and create a new C# file named ‘InputManager.cs’. Since our class will use the Drag behavior, let’s apply the necessary libraries and interfaces to our class as follows. The drag behavior can be used on both computers and mobile devices without any problem, but we could also use the Touch class if we wanted it to be used only on devices that support touch. By the way, do not forget to import the EventSystems library to our script.
After adding the IDragHandler, IBeginDragHandler and IEndDragHandler interfaces, our IDE will warn us to implement the methods required by these interfaces. Let’s click on the error that pops up and click on “Implement the interface” button.
OnBeginDrag method is called when a drag behavior starts on the screen, the OnDrag method will be called while the drag behavior proceeds and lastly, the OnEndDrag method is called on the frame where the drag behavior ends.
We will operate our swipe gesture with 4 directions as up, down, right and left. For these purpose, creating an enum named Direction will benefit us later in our project.
Now let’s add our global variables. A direction for our drag behavior, start position, end position, swipe threshold (minimum distance between start and end points to perform the drag behavior), and one bool variable to control whether dragging has started.
Let’s assign the initial values to our variables in Awake method.
When the drag behavior starts, our draggingStarted value should be set to true, and our startPos variable should be assigned as the coordinates of the point where the event occurred.
During the drag behavior, if our draggingStarted value is true, we assign our end position as the current position of our Pointer, and we get the difference. If the magnitude of the difference vector is greater than the swipe threshold, we begin the direction determination process. If the x component of the difference vector is greater than the y component, we do a horizontal swipe.
Otherwise we do a vertical swipe. In horizontal swipe, if x component is greater than zero, our direction will become Right, otherwise Left. In vertical swipe, if y is greater than zero, our direction will become Up, otherwise Down. If our drag cannot exceed the swipe threshold, we set our direction value as None.
When our drag behavior ends, if the draggingStarted value is true and our direction value is different from None; It indicates that we have obtained one of the four directions and that we can use this data in our game as we want. I chose to print the direction value into Debug console, but you can use it in your game as you wish.
The complete code is as follows:
NOTE: Since Pointer events triggers on the UI objects, do not forget to attach this script into a Panel object which covers the entire screen.
So, Swipe detection in Unity was basically like this. Of course, it can be implemented in various ways depending on your needs and creativity. I hope this article had been useful for you. Stay tuned!