Unity Quick Tip — Implementing a Simple Screen Swipe on Mobile using the New Input System
This tutorial was tested in Unity 2022.3.10.
In this article, I will guide you through the process of implementing a screen swipe feature for mobile devices using Unity’s new Input System.
Setting up the Input System:
Go to Window > Package Manager and ensure that “Unity Registry” is selected in the Packages dropdown.
Select “Input System” and click “Install” to install the Input System package.
Once the package is installed, you’ll receive a warning that the Input System backends need to be enabled. Click “Yes” to enable them, and this will restart your editor.
Configuring Input Debugger:
Click the Options dropdown and enable “Simulate Touch Input From Mouse or Pen.”
The next step is to change the Input Debugger to “Simulate Touch Input From Mouse or Pen”. This is so we can test using the mouse in game mode to simulate touches.
Go to Window > Analysis > Input Debugger, then click the Options dropdown and enable “Simulate Touch Input From Mouse or Pen”.
To set up the environment for testing in the editor you can switch your game mode over to simulator and pick your device from the drop down, I have chosen the Samsung Galaxy S10+.
We now need to set up our input actions. The plan is that we will track the delta of the finger movement, process it when the touch is complete to determine if the magnitude of the swipe is large enough for us to consider the movement as a swipe.
In your project view click the plus buttons and select “Input Actions” to create a new InputAction Asset. I called mine “PlayerControls”.
Double click on your InputActions asset to open it. We need to create a new action map by clicking the plus button, I called mine “Player”.
We now need to set up two Actions, one will handle when a touch and the other will track the delta of our finger swipe.
Change the name of the first action to “Touch” by double clicking on it.
To set up the Touch action, Under “Action” change the Action Type to “Button” if it is not already selected.
Change the binding by clicking the arrow on the Touch Action, then clicking on the Biding section > Path input box > Touch Screen > Press.
Note: In Unity 2022.3.x, the Path Input Box may not respond well to clicks on the right-hand side of the window. For better results, click to the left-hand side of your selection.
Create the Swipe Action by clicking the “+” button in the Actions area and renaming the new Action to “Swipe.”
Change the Action Type to “Value” and the Control type to “Vector 2.”
Change the binding to Touch Screen > Delta, save the asset changes.
Click on your InputAsset and check the “Generate C# Class” checkbox and click “Apply”, this give us the ability to access the InputActions via script.
Implementing the actions:
Create a new script (e.g., SwipeTest) and open it.
You’ll need the following variables:
_controls: This is where all of our input will be received from.
minimumSwipeMagnitude: The minimum magnitude the player's finger must reach before we register a swipe.
_swipeDirection: This is where we store the swipe delta direction.
Start method, create a new instance of the player controls and assign it to your variable. Then, enable the Player action map and subscribe to the relevant events.
Swipe.performed event is triggered when the delta changes due to touch screen finger movement.
Touch.cancelled event is called when the player lifts their finger. You will check the magnitude of the delta (Swipe action) at this point to determine if the swipe was strong enough.
Testing the Functionality:
To test this functionality, create a cube in the scene and attach the new script cube. In my scene, I’ve added two cubes — one to hold the initial position (which I’ve slightly shrunk) to help demonstrate the movement.
Now we can test our functionality.
That’s all for now.