- Home /
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;
}
}
}
Your question is not really clear, I will assume that you want the swipe to be activated without releasing the touch, see my answer below.
Also you should remove some blank line in your code for better readability.
Answer by PhilRousse · Nov 29, 2013 at 02:42 PM
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.
hi thanks for your reply your code is working. Can you create more flexible like daring dash game
What do you mean by "daring dash game"?
That code is for Swipe Up. It's base on the code that you've write.
if( (currentSwipe.magnitude > $$anonymous$$inSwipeDistance) && (currentSwipe.y > 0) && ($$anonymous$$ath.Abs(currentSwipe.x) < SwipeAllowedVariance))
At line 27:
If you change
(currentSwipe.y > 0)
by(currentSwipe.y < 0)
it will be trigger to swipe down.If you switch `currentSwipe.y` and `currentSwipe.x`, it will trigger to horizontal swipe. x > 0 = Right and x < 0 = left.
ya i know sorry it is danger dash game. Now it is working fine like that game thanks for correcting me
hi there is a slight problem in this code swipe is working with out releasing my finger but it is working more then once when i swipe my finger and stop with out releasing and swipe again with out releasing my finger then action is repeating twice can you fix this problem action should be once per swipe with out releasing my finger here is the following code that i used
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.$$anonymous$$oved)
{
//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;
}
}
}
Your answer

Follow this Question
Related Questions
Swipe Gesture iOS 1 Answer
Controlling Animation ON TOUCH 0 Answers
Detect if finger lifted off screen 1 Answer
Android how to use swipe to make certain part of an object fade out? 0 Answers
swipe camera movement wont stay between limitations 1 Answer