- Home /
Swipe functions for android.?
Hello, I am new to unity.I need java script for applying LeftSwipe,RightSwipe,UpSwipe for android.I want my character to rotate 90 degrees right on swiping right,similarly 90 degrees left on swiping left and to jump on swiping up.Can anybody please help me.I am trying to find this since one day...Thanks in advance.
Answer by Andre Barbosa · Aug 05, 2013 at 05:34 PM
Swipe, along with other touch based input, is easy to do, but hard to do right.
If you are in hurry, getting a plugin is probably the best advice I can give.
I have used Finger Gestures and it is great:
is there any free plugins,as i cant afford paid one in unity assets.?..I just want script for swiping..!
Answer by lancer · Aug 05, 2013 at 05:39 PM
Open up the scripting reference and look up "Input" All the swipes are handled in there.
Answer by RickHurd · Jan 24, 2014 at 03:20 AM
Here is a reference script I found, hope it helps :D
/* this variable is used to make use of the swipe. Once a swipe is detected
* a function called Swipe(swipeType : Vector2) is called
*/
var controller : PlayerController;
// variables:
private var fingerStartTime : float = 0.0;
private var fingerStartPos : Vector2 = Vector2.zero;
private var isSwipe : boolean = false;
private var minSwipeDist : float = 50.0;
private var maxSwipeTime : float = 0.5;
// main function:
function Update()
{
if (Input.touchCount > 0)
{
for (touch : Touch in Input.touches)
{
switch (touch.phase)
{
case TouchPhase.Began :
/* this is a new touch */
isSwipe = true;
fingerStartTime = Time.time;
fingerStartPos = touch.position;
break;
case TouchPhase.Canceled :
/* The touch is being canceled */
isSwipe = false;
break;
case TouchPhase.Ended :
var gestureTime : float = Time.time - fingerStartTime;
var gestureDist : float = (touch.position - fingerStartPos).magnitude
if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist)
{
var direction : Vector2 = touch.position - fingerStartPos;
var swipeType : Vector2 = Vector2.zero;
if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)
{
// the swipe is horizontal:
swipeType = Vector2.right * Mathf.Sign(direction.x);
}
else
{
// the swipe is vertical:
swipeType = Vector2.up * Mathf.Sign(direction.y);
}
controller.Swipe(swipeType);
}
break;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Android Gestures/Swipe Captures 1 Answer
Input.GetTouch never getting TouchPhase.Ended on Android 0 Answers
Object position resets to Camera Position using touch 1 Answer
Swipe control for circle (Can do swipe left/right) 2 Answers
using OnMouseDown instead of touch functions for touch devices 1 Answer