- Home /
Unity 2D. How to swipe without detecting tap?
So basically what I want to do is perform a jump (it has double jump as well) on tap and a dash on swipe right, but if I do it with "`if (Input.GetMouseButtonDown(0))`", then when I start swiping it detects a tap as well, so it means another jump. My solution was the one I pasted below, but the problem with it is that if I do it this way it only jumps when I release my finger so it looks and feels laggy. If you guys have any solution or better workaruond for this, let me know! Thank You!
if (Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
firstPressPos = new Vector2(t.position.x, t.position.y);
}
if (t.phase == TouchPhase.Ended)
{
secondPressPos = new Vector2(t.position.x, t.position.y);
if (firstPressPos == secondPressPos)
{
//Perform jump
}
currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
currentSwipe.Normalize();
//swipe right
if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f && grounded == false && dash == 1 && paused == false)
{
Debug.Log("right swipe");
//Perform dash
}
}
}
Answer by madks13 · Aug 06, 2018 at 10:57 AM
I'm not expert on touch inputs, but can't you use Stationary and Moved touch phases rather than precise positioning? Stationary would mean press, and Moved would mean swipe.
Answer by keckluis · Aug 06, 2018 at 01:56 PM
if(Input.GetTouch(0).phase == TouchPhase.Stationary) {
//jump
}
if(Input.GetTouch(0).phase == TouchPhase.Moved) {
//dash
}