- Home /
Make Swipe Detection Longer?
Hello Unity Answers. I know this might be sort of a do this for me question, but I just want to know something about this swipe code I was lucky enough to find after searching the web for a while. You see everything works good, however there are some problems. The swipe script has to work with another script of mine that allows me to jump when I tap anywhere on the screen. So when I sometimes tap, it detects it as a swipe and does not jump. This leads me to die many times on my game. What I want to know, is how to make the swipe the detection longer before it finally detects it as a swipe? So like I would have to swipe for a longer time. Right now just the slightest swipe down, it detects it. So how can I make it longer? Here's the script:
public class SwipeDetector : MonoBehaviour {
// Values to set:
public float comfortZone = 70.0f; //was 70.0f
public float minSwipeDist = 14.0f; //was 14.0f //17.0f seemed to do nothing //25.0f
public float maxSwipeTime = 0.12f; //was 0.5f sucks //2 one was 0.8f ok but still not //3 was 0.10f good but not without problems //0.12f feels the same //0.15f seems worse //0.17f ok //0.13f
public GameObject Test;
private float startTime;
private Vector2 startPos;
private bool couldBeSwipe;
public enum SwipeDirection {
None,
Up,
Down
}
public SwipeDirection lastSwipe = SwipeDetector.SwipeDirection.None;
public float lastSwipeTime;
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
lastSwipe = SwipeDetector.SwipeDirection.None;
lastSwipeTime = 0;
couldBeSwipe = true;
startPos = touch.position;
startTime = Time.time;
break;
case TouchPhase.Moved:
if (Mathf.Abs(touch.position.x - startPos.x) > comfortZone)
{
Debug.Log("Not a swipe. Swipe strayed " + (int)Mathf.Abs(touch.position.x - startPos.x) +
"px which is " + (int)(Mathf.Abs(touch.position.x - startPos.x) - comfortZone) +
"px outside the comfort zone.");
couldBeSwipe = false;
}
break;
case TouchPhase.Ended:
if (couldBeSwipe)
{
float swipeTime = Time.time - startTime;
float swipeDist = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if ((swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist)) //was &&
{
// It's a swiiiiiiiiiiiipe!
float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
// If the swipe direction is positive, it was an upward swipe.
// If the swipe direction is negative, it was a downward swipe.
if (swipeValue > 0)
lastSwipe = SwipeDetector.SwipeDirection.Up;
else if (swipeValue < 0)
lastSwipe = SwipeDetector.SwipeDirection.Down;
// Set the time the last swipe occured, useful for other scripts to check:
lastSwipeTime = Time.time;
Test.setActive(true);
}
}
break;
}
}
}
}
As you can tell by the // notes. I already messed around with the values. You can read the notes to see my results.
Wouldn't it be better, if you combined both scripts, since swipe and tap can not coexist, and you could easily differ them by conditions? That's really my opinion.
In your situation, conditions for swipe and tap must be conflicting, the only difference between tap and swipe can be $$anonymous$$SwipeDist, but i guess you are not checking the same value for tap.
Also you can put a $$anonymous$$SwipeTime, since swipes are not usually that fast.
"Wouldn't it be better, if you combined both scripts, since swipe and tap can not coexist, and you could easily differ them by conditions?" Possibly yes, not quite sure on how to go about that. The script that I use for the tap is part of the Character Controller which is already big enough. I'm guessing you mean like checking if($$anonymous$$SwipeTime == 10) Swipe and else, jump. Little confusing to me but I see what you're saying.
By the way, what value do you think would be good for the $$anonymous$$SwipeTime? Originally the maxSwipeTime was 0.5f. now it's 0.12f since it seems to work better.