- Home /
How to recognize a (downwards) swipe gesture
I want the object to do such and such if the screen is swiped downwards anywhere on the screen. C# please! thanks in advance!
Answer by Derek-Wong · Jan 25, 2015 at 07:23 AM
Here is the idea for you:
detect the player have touch the screen (with one finger? or 2? or 3? I assume single, but you can set it to something else)
catch the initial pos of the finger
catch the new pos of the finger in update
a simple "if" to detect if the finger has move its pos Y
so here is the code:
private float initialPosY;
if(Input.touchCount == 1){
var finger1 = Input.GetTouch(0);
if (finger1.phase == TouchPhase.Began && finger2.phase == TouchPhase.Began){
initialPosY = finger1.position.y;
}else if (finger1.phase == TouchPhase.Moved){
float newPosY = finger1.position.y;
if(newPosY-initialPosY < xxxx){ //xxxx is the distance you define it as a swipe,
//do something
}
}
}
if I am correct, upper Y should be larger, so the XXXX should be negative, anyway, please try it yourself.
do you think you could show me this in C#? almost all of it makes sense but i don;t know how to make var finger1 in C#
you can actually create a var in c#, and I am using c# :D
Aaaahh... Interesting! Alright, I'll give it a go. Thanks!
Your answer
Follow this Question
Related Questions
Detect swipe up gesture IOS 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Swipe to move player 1 Answer