in Game Dev+Unity

How to Detect Swipes in Unity


While working on the Endless Runner Kit and Run Sheeda Run (both products of we.R.play), the first thing to do was to detect swipe inputs to get the basic controls working. When a user swipes on the screen they swipe in an angle starting from the starting to the ending point of their finger’s contact on the screen and more often then not they make an arc with their finger.

You can just go ahead and use the code given below and you’ll be good to go but if you want to know how it works, you’ll have to think a little about analytic geometry to understand it. I assume that the user will make an angle in the Cartesian plane starting from the origin. The origin will be where ever the user starts the swipe from. As you know the Cartesian plane is divided into four quadrants which won’t be enough to detect swipes in four directions so what we do is draw a line has the slope “1” going through the origin and another line has the slope “-1” also going through the origin. Now you have divided the plane into the eight parts.

 

Starting from the origin, if the user swipes into the section 1 & 2, that would mean an “up” swipe, section 3 & 4 will cover the left swipe, 5 & 6 covers the downwards swipe and finally 7 & 8 cover the right swipe. This way you have covered quite a bit of angle that the user can make. An endless runner genre game is one example where this input would be useful.

Here is the code in C#:

 

Share
Share this Post
Your URL copied
Comments on This Post
Let us know your thoughts about this topic

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

Top