Swipe not always detected

Dear Unity, As a 3D Game Designer and a total c# noob I need some help :wink:

Some of my gestures are not always detected. Could somebody look into the code?

{

        rigidbody.AddForce(speed, ForceMode.Force);
        foreach (Touch t in Input.touches)
        {
            if (t.phase == TouchPhase.Began)
            {
                initialTouch = t;
            }
            else if (t.phase == TouchPhase.Moved && !hasSwiped)
            {
                float deltaX = initialTouch.position.x - t.position.x;
                float deltaY = initialTouch.position.y - t.position.y;
                distance = Mathf.Sqrt(deltaX * deltaX) + (deltaY * deltaY);
                bool swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);

                if (distance > 80f)
                {
                    if (swipedSideways && deltaX > 0) //S Left
                    {
                        //Action on left Swipe
                       // rigidbody.AddForce(SwipeLeft, ForceMode.VelocityChange);
                        Debug.Log("Swipe Left");

                    }
                    else if (!swipedSideways && deltaX <= 0) //S Right
                    {
                        //Action on Right Swipe
                        //rigidbody.AddForce(SwipeRight, ForceMode.VelocityChange);
                        Debug.Log("Swipe Right");
                    }
                    else if (!swipedSideways && deltaY > 0) //S Down
                    {
                        //Action on Swipe Down
                        Debug.Log("Swipe Down");
                    }
                    else if (!swipedSideways && deltaY <= 0) //S Up
                    {
                        // Action on Swipe Up
                        //rigidbody.AddForce(SwipeUp, ForceMode.VelocityChange);
                        Debug.Log("Swipe Up");
                    }
                    hasSwiped = true;
                }
            }
            else if (t.phase == TouchPhase.Ended)
            {
                initialTouch = new Touch();
                hasSwiped = false;
            }
        }
    }

A few things:

For swipe right

else if (!swipedSideways && deltaX <= 0)

Should be

else if (swipedSideways && deltaX < 0)

and for swipe down

else if (!swipedSideways && deltaY <= 0)

Should be

else if (!swipedSideways && deltaY < 0)

If deltaX or Y is 0 then no swipe. You shouldn’t test for that case to say a swipe happened.