- Home /
Swipe function
Hi, I am doing a swipe function for a football game. How do I get the following items?
StartSwipeTime
EndSwipeTime
StartSwipePoint/Coordinate
EndSwipePoint/Coordinate
I have the direction of the swipe --> gesture.swipeVector.normalized. What is the function to move the object based on speed of swipe, length of swipe and direction of swipe?
Thanks!
Check out prime31's gesture kit. It was called touch kit. They have a swipe script built and all you have to do is add the kit to your game and put the swipe script on an empty game object. It is easy to modify the script to your liking as well. The best thing about it is that it's free.
Hi highpockets,
I have Easytouch which is better than Touch $$anonymous$$it. I just want to know the unity function to find these parameters. Thanks!
No prob, it just has everything that you're asking for (touch input, start time/end time, coordinates ( which are easy to translate to world space if you have the z point reference in the world space). It also has touch possible, began, ended as tanoshimi mentions below, but I haven't tried easy touch
For moving an object based on your swipe direction... You could have booleans for each direction if you just want up, down, left, right. For more acute control, you can get access to the point In the world in relation to your touch position on screen take a look at Camera.main.ScreenPointToRay( your touch.position goes here ). The X and Y from your touch position on screen are used in world space, you just have to declare the z position ( your objects z position ). If all you want is the direction, that won't be necessary, all you would need then is to $$anonymous$$us your end position as a vector with the start position as a vector and the resulting vector is your direction. The speed (velocity) would be calculated by the $$anonymous$$athf.Abs(startPoint - EndPoint) and divide that by ( endTime - startTime ) of the swipe (touchphase.Began/touchphase.Ended)
Hope that helps.
Hi highpockets,
I manage to get all the parameters I need (speed, angle and distance). But now I do not know to implement these parameters to move the object using my swipe.
I need help on how to create functions to animate the object based on the 3 parameters.
Thanks!
Answer by tanoshimi · Sep 23, 2013 at 07:53 AM
All touch input is handled through Input.touches. For swipe gestures, you want to consider the touchphase.Began and touchphase.Ended phases of each touch.
To determine the start/end time of a swipe, record the time at which these phases occur (measured since game start) using Time.time.
For coordinates, look at the position of the touch, which gives coordinates as a Vector2
Hi Tanoshimi,
Thanks for the info! I manage to get the coordinates. For Time.time, can you give me an example. Like this,
//called when DragStarts
void On_DragStart( Gesture gesture){
//store Time.time here. What is the syntax? Variable is startTime (what is this variable type?)
}
Lastly, do you know any move functions with parameters moveSpeed/magnitude, distanceTravelled and direction?
Time.time is a float variable, with the fractional number of seconds since the start of the game. So, if you want to compare the speed of a gesture, for example, you'll need to use two variables to record "gesturestarttime" and "gestureendtime" (which you set equal to Time.time in the touchphase.Began and touchphase.Ended phases respectively). Then just subtract the starttime from the endtime to calculate how long the gesture took.
Answer by TifaOng · Oct 01, 2013 at 08:39 AM
I found my answers, here's the code for reference. Note: I'm using Easytouch.
void On_DragStart(Gesture gesture){
// Verification that the action on the object
if (gesture.pickObject == gameObject){
position = gesture.GetTouchToWordlPoint(13);
}
dragStartPos = position;
dragTimeBegin = Time.time;
} //same with On_DragEnd.
Then Vector3.Distance (a,b) to get the distance and endTime - startTime to get the time :)
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613114642im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer