# How to make swipe function?

i am trying to do swipe functionality for android phone and the code is working but i am facing a problem when i am swiping and removing my finger then it is doing action or else with out removing my finger on device the action can not be done what i want to do can any one help me with this here is my code

``````if(Input.touches.Length > 0)

{

Touch t = Input.GetTouch(0);

if(t.phase == TouchPhase.Began)

{

//save began touch 2d point

firstPressPos = new Vector2(t.position.x,t.position.y);

}

if(t.phase == TouchPhase.Ended)

{

//save ended touch 2d point

secondPressPos = new Vector2(t.position.x,t.position.y);

//create vector from the two points

currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);

//normalize the 2d vector

currentSwipe.Normalize();

//swipe upwards

if(currentSwipe.y > 0 && currentSwipe.x > -0.8f && currentSwipe.x < 0.8f)

{

lastJumpButtonTime = Time.time;

}

}

}
``````

You need to track the swipe on the `TouchPhase.Moved` to acheive what you want. Here your code rewritten:

``````//Constant use in code. Use these to tweak the result
float MinSwipeDistance = 35f;
float SwipeAllowedVariance = 0.8f;
bool trackSwipe = false;

if(Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);

if(t.phase == TouchPhase.Began)
{
//save began touch 2d point
firstPressPos = new Vector2(t.position.x,t.position.y);
trackSwipe = true;
}

if(t.phase == TouchPhase.Moved)
{
//save ended touch 2d point
secondPressPos = new Vector2(t.position.x,t.position.y);

//create vector from the two points
currentSwipe = secondPressPos - firstPressPos;

//swipe upwards
//If the swipe is long enough and in the right direction.
if( (currentSwipe.magnitude > MinSwipeDistance) && (currentSwipe.y > 0) && (Math.Abs(currentSwipe.x) < SwipeAllowedVariance))
{
lastJumpButtonTime = Time.time;
//We've reconized the swipe. Stop tracking it.
trackSwipe = false;
}
}

if(t.phase == TouchPhace.Ended)
{
trackSwipe = false;
}
}
``````

For vector math, you don’t have to substact each Vector element to create a delta vector, you can do it by subtracting a vector by another.

If you need more explanation, please comment.