Swipe movement not working correctly.

Hi, I have an object, controlled by the player via a custom script, and when swipe is detected, control is taken away from the player and it moves along in a straight line until a collision occurs, where the object stops and control is returned to the player. The script, a modified version of one found on the internet, records the first and last position of a swipe and then compares them in various ways to find if it was an up, down, left or right swipe. the problem I am having is that the swipe detection is very weak: sometimes it ends the swipe early, and it often does not recognise the swipe whatsoever. I assumed this was something to do with the swipeThresh variable, however changing this does not perfect the movement. I’m looking for something that works consistently. Is there anything that can be done with this script to make it reliable, or is there another way of doing it? Thanks in advance, Tom.

    // Declaration of Variables

        //These are the movement booleans. When one of these are true, the Player Object moves in that direction.
        var Right: boolean;
        var Left: boolean;
        var Up: boolean;
        var Down: boolean;
        //This is the speed of the Player Object.
        var MoveSpeed: float = 7;
        //This is so we can move the position of the Collider Mesh of the Player Object around.
        var myCollider: BoxCollider;
        //Stop Screen switching off.
        Screen.sleepTimeout = 0;
        //Swipe Movement Vars
 var  swipeThresh      :    float  =   1.2;
 var  swipeStart       :  Vector2  =   Vector2.zero;
 var  swipeEnd         :  Vector2  =   Vector2.zero;
 var  swipeWasActive   :  boolean  =   false;
 var  Subtraction      :  Vector2;
 
 
//Movement 

 function Update ()
 {
 
     if ( Input.touchCount == 1 ) {
         processSwipe();
         }
 
 
//Activating the Booleans

 //Swipe
 if (Subtraction.x > 0 && Mathf.Abs(Subtraction.x) > Mathf.Abs(Subtraction.y)&& !Left && !Up && !Down && myCollider.center!=Vector3(0,0,0.15)){
 Right = true;
 }
    if (Subtraction.x < 0 && Mathf.Abs(Subtraction.x) > Mathf.Abs(Subtraction.y) && !Right && !Up && !Down && myCollider.center!=Vector3(0,0,-0.15)){
 Left = true;
 }
     if (Subtraction.y > 0 && Mathf.Abs(Subtraction.y) > Mathf.Abs(Subtraction.x) && !Left && !Right && !Down && myCollider.center!=Vector3(-0.15,0,0)){
 Up = true;
 }
     if (Subtraction.y < 0 && Mathf.Abs(Subtraction.y) > Mathf.Abs(Subtraction.x) && !Left && !Up && !Right && myCollider.center!=Vector3(0.15,0,0)){
 Down = true;
 }
 
 //Arrow Keys
    //If the User presses the key, no other Boolean is true and the Collision Mesh of the Player Object
    //is not already at the specified direction, activate the boolean.
    
     if(Input.GetKey("right") && !Left && !Up && !Down && myCollider.center!=Vector3(0,0,0.15)){
     Right = true;
     }
 
     if(Input.GetKey("left") && !Right && !Up && !Down && myCollider.center!=Vector3(0,0,-0.15)){
     Left = true;
     }
 
     if(Input.GetKey("up") && !Left && !Right && !Down && myCollider.center!=Vector3(-0.15,0,0)){
     Up = true;
     }
     
     if(Input.GetKey("down") && !Left && !Up && !Right && myCollider.center!=Vector3(0.15,0,0)){
     Down = true;
     }
    
    
//Actual Movement
//If the Boolean is true, move the Player Object and the Collision Mesh.
 
 //Right
        if (Right) {
        transform.Translate(Vector3(0,0,MoveSpeed) * Time.deltaTime);
        myCollider.center = Vector3(0,0,0.15);
        }

 //Left

        if (Left) {
        transform.Translate(Vector3(0,0,-MoveSpeed) * Time.deltaTime);
        myCollider.center = Vector3(0,0,-0.15);
        }

 //Up

        if (Up) {
        transform.Translate(Vector3(-MoveSpeed,0,0) * Time.deltaTime);
        myCollider.center = Vector3(-0.15,0,0);
        }

 //Down

        if (Down) {
        transform.Translate(Vector3(MoveSpeed,0,0) * Time.deltaTime);
        myCollider.center = Vector3(0.15,0,0);
        }
 }
 
 function OnGUI ()
 {
     processSwipe();
     drawStartBox();
     drawEndBox();
 }
 
 function processSwipe ()
 {
     if ( Input.touchCount != 1 ) {
     Subtraction = swipeEnd - swipeStart;
         return;
     }
 
     var theTouch : Touch = Input.touches[0];
 
     /* skip the frame if deltaPosition is zero */
 
     if ( theTouch.deltaPosition == Vector2.zero ) {
         return;
     }
 
     var speedVec : Vector2 = theTouch.deltaPosition * theTouch.deltaTime;
     var theSpeed :   float = speedVec.magnitude;
 
     var swipeIsActive : boolean = ( theSpeed > swipeThresh );
 
     if ( swipeIsActive ) {
 
         if ( ! swipeWasActive ) {
             swipeStart = theTouch.position;
         }
     }
 
     else {
 
         if ( swipeWasActive ) {
             swipeEnd = theTouch.position;
             Debug.Log("Swipe Complete");
         }
     }
 
     swipeWasActive = swipeIsActive;
 }
 
 function drawStartBox ()
 {
     if ( swipeStart == Vector2.zero ) {
         return;
     }
 
     /* don't forget to invert the y-coordinate */
 
     var theY = Screen.height - swipeStart.y;
     var theX = swipeStart.x;
 
     var theW = 140;
     var theH =  40;
 
     var theRect : Rect = Rect(theX, theY, theW, theH);
 
     GUI.Label(theRect, "Start");
 }
 
 function drawEndBox ()
 {
     if ( swipeEnd == Vector2.zero ) {
         return;
     }
 
     /* don't forget to invert the y-coordinate */
 
     var theY = Screen.height - swipeEnd.y;
     var theX = swipeEnd.x;
 
     var theW = 140;
     var theH =  40;
 
     var theRect : Rect = Rect(theX, theY, theW, theH);
 
     GUI.Label(theRect, "End");
 }

Well i had a similar problem with my Swipe function, and a made a very simple script that worked pretty well for me(up,down,left,right), i record the first touch point and during the movement of the finger i am checking the directions of the vector(first point and last) relatively to the first touch point and depending on the angle, i get the actual direction:

        Vector2 deltaPosition;
        Vector2 afterDeltaPosition;
        float angle;
        
        void FixedUpdate()
        {
            if(Input.touchCount == 1)
            { 
                //its always x=-1 and y=-1 if touchCount == 0 Resets
                if(deltaPosition.x == -1 && deltaPosition.y == -1)
                    deltaPosition = Input.GetTouch(0).position; //First touch  point stored
                afterDeltaPosition = Input.GetTouch(0).position;//the swiping points
                angle = Mathf.Atan2(afterDeltaPosition.y-deltaPosition.y,afterDeltaPosition.x -deltaPosition.x)* 180/Mathf.PI ; // angle in degrees from 0,-180 and 0,180)
                Debug.Log("Angle "+ angle);
                if(angle < 45f && angle > -45f)
                {
                    if(anim.isPlaying() && anim.CurrentClip.name != "right")
                {
                anim.Play("right"); 
                rigidbody.velocity = (Vector3.right * speed);
            }
        }
        else if(angle < 135f && angle > 45f)
        {
            if(anim.isPlaying() && anim.CurrentClip.name != "front")
            {
                anim.Play("back"); 
                rigidbody.velocity = (Vector3.up * speed);
            }
        }
        else if(angle > 135f || angle < -135f)
        {
            if(anim.isPlaying() && anim.CurrentClip.name != "left")
            {
                anim.Play("left"); 
                rigidbody.velocity = (Vector3.left * speed); 
            }
        }
        else if(angle > -135f && angle < -45f)
        {
            if(anim.isPlaying() && anim.CurrentClip.name != "front")
            {
                anim.Play("front"); 
                rigidbody.velocity = (Vector3.down * speed);
            }
        }
    }
    else if( Input.touchCount == 0)
    {
        if(deltaPosition.x != -1 && deltaPosition.y != -1)
        {
            deltaPosition.x=-1;
            deltaPosition.y=-1;
        }
    }
}

The code here works very, very well and I thank you for giving me such a solid piece of code, however there is one problem; the player always wants to move to the right. I think this is due to setting the deltas to minus one, however I am unsure. Could you perhaps look and see if you can tell where I’m going wrong? Thanks again for all of your help, Tom.

//Movement
//These are the movement booleans. When one of these are true, the Player Object moves in that direction.
var Right: boolean;
var Left: boolean;
var Up: boolean;
var Down: boolean;
//This is the speed of the Player Object.
var MoveSpeed: float = 14; 
//This is so we can move the position of the Collider Mesh of the Player Object around.
var myCollider: BoxCollider;

//Swipe
var deltaPosition: Vector2;
var afterDeltaPosition: Vector2;
var angle: float;

function FixedUpdate()
{    
    if(Input.touchCount == 1)
    {
        //its always x=-1 and y=-1 if touchCount == 0 Resets
        if(deltaPosition.x==-1&& deltaPosition.y==-1)
            deltaPosition = Input.GetTouch(0).position; //First touch point stored.
        afterDeltaPosition = Input.GetTouch(0).position;//The swiping points.
        angle = Mathf.Atan2(afterDeltaPosition.y-deltaPosition.y,afterDeltaPosition.x -deltaPosition.x)* 180/Mathf.PI ; // angle in degrees from 0,-180 and 0,180)
        Debug.Log("Angle"+ angle);
        
        if(angle < 45f && angle > -45f)
        {
            if (!Left && !Up && !Down && myCollider.center != Vector3(0,0,0.15))
            { 
                Right = true;
                deltaPosition.x = -1; 
                deltaPosition.y = -1;
                afterDeltaPosition.x = 0;
                afterDeltaPosition.y = 0;
                Moving.Play();
            }
        }
        else if(angle < 135f && angle > 45f)
        {
            if (!Left && !Up && !Right && myCollider.center != Vector3(0.15,0,0))
            { 
                Up = true;
                deltaPosition.x = -1; 
                deltaPosition.y = -1;
                afterDeltaPosition.x = 0;
                afterDeltaPosition.y = 0;
                Moving.Play();
            }
        }
        else if(angle > 135f || angle < -135f)
        {
            if (!Right && !Up && !Down && myCollider.center != Vector3(0,0,-0.15))
            { 
                Left = true;
                deltaPosition.x = -1; 
                deltaPosition.y = -1;
                afterDeltaPosition.x = 0;
                afterDeltaPosition.y = 0;
                Moving.Play();
            }
        }
        else if(angle > -135f && angle < -45f)
        {
            if (!Left && !Right && !Down && myCollider.center != Vector3(-0.15,0,0))
            { 
                Down = true;
                deltaPosition.x = -1; 
                deltaPosition.y = -1;
                afterDeltaPosition.x = 0;
                afterDeltaPosition.y = 0;
                Moving.Play();
            }
        }
    }
    else if( Input.touchCount == 0)
    {
        if(deltaPosition.x != -1 && deltaPosition.y != -1)
        {
            deltaPosition.x = -1;
            deltaPosition.y = -1;
        }
    }
}

function Update ()
{
    //Arrow Keys
    //If the User presses the key, no other Boolean is true and the Collision Mesh of the Player Object
    //is not already at the specified direction, activate the boolean.
    if(Input.GetKey("right") && !Left && !Up && !Down && myCollider.center != Vector3(0,0,0.15))
    {
        Right = true;
        Moving.Play();
    }
    
    if(Input.GetKey("left") && !Right && !Up && !Down && myCollider.center != Vector3(0,0,-0.15))
    {
        Left = true;
        Moving.Play();
    }
    
    if(Input.GetKey("up") && !Left && !Right && !Down && myCollider.center != Vector3(-0.15,0,0))
    {
        Up = true;
        Moving.Play();
    }
    
    if(Input.GetKey("down") && !Left && !Up && !Right && myCollider.center != Vector3(0.15,0,0))
    {
        Down = true;
        Moving.Play();
    }
 
    //Actual Movement
    //If the Boolean is true, move the Player Object and the Collision Mesh.
    if (Right)
    {
        transform.Translate(Vector3(0,0,MoveSpeed) * Time.deltaTime);
        myCollider.center = Vector3(0,0,0.15);
    }
    
    if (Left)
    {
        transform.Translate(Vector3(0,0,-MoveSpeed) * Time.deltaTime);
        myCollider.center = Vector3(0,0,-0.15);
    }
    
    if (Up)
    {
        transform.Translate(Vector3(-MoveSpeed,0,0) * Time.deltaTime);
        myCollider.center = Vector3(-0.15,0,0);
    }
    
    if (Down)
    {
        transform.Translate(Vector3(MoveSpeed,0,0) * Time.deltaTime);
        myCollider.center = Vector3(0.15,0,0);
    }
}