- Home /
Question by
AndrewBilotti · Feb 15, 2016 at 06:36 PM ·
touchswipetap
Detect whether a tap is a swipe up gesture or just a tap
Hello fellow Unity coders. I have worked for a long time trying to figure out code to detect if when there is a tap, is it a tap, or is it a swipe up gesture? Can somebody please give me a snippet of code to do this:
if (swipe up) {
jump();
} else if (just tap) {
shoot();
}
Thank you very much! BTW, I am using C# :D
Comment
Will this work? I don't have a Apple developer's ID yet :)
public touch touch;
public bool onetouch = false;
if (Input.touchCount > 0 && Input.touchCount != null) {
if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.$$anonymous$$oved || touch.phase == TouchPhase.Stationary) {
if (touch.position.x > touch.deltaPosition.x + 3 && touch.position.x < touch.deltaPosition.x - 3 && onetouch == false) { //3px accuracy, stationery :P
jump ();
onetouch = true; // so there is only 1 jump per touch
}
else if (touch.position.x <= touch.deltaPosition.x + 3 && touch.position.x >= touch.deltaPosition.x - 3 && onetouch == false) { //3px accuracy, vertical swipe up
Shoot ();
onetouch = true; // so there is only 1 bullet per touch
}
}
}
//Now finger is off screen, so they can shoot/tap again
if ((touch.phase == TouchPhase.Ended) || (touch.phase == TouchPhase.Canceled)) {
onetouch = false;
}
Your answer
Follow this Question
Related Questions
Tap for variable height jump, swipe to dash 1 Answer
Unity 2D. How to swipe without detecting tap? 2 Answers
Differentiating a tap from a swipe? 1 Answer
Single Tap Registered on Swipe (C#) 0 Answers
Custom touch phase? 0 Answers