Last active
August 29, 2015 14:09
-
-
Save StelianMorariu/3615f0e23c40c3257ad0 to your computer and use it in GitHub Desktop.
Detect swipes in Unity 3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
var minSwipeDistY : float; | |
var minSwipeDistX : float; | |
var Swipe: GUIText; | |
private var startPos :Vector2; | |
function Update () | |
{ | |
if (Input.touchCount > 0) | |
{ | |
var touch : Touch = Input.touches[0]; | |
switch (touch.phase) | |
{ | |
case TouchPhase.Began: | |
startPos = touch.position; | |
break; | |
case TouchPhase.Ended: | |
var swipeDistVertical : float; | |
swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude; | |
if (swipeDistVertical > minSwipeDistY) | |
{ | |
var swipeValue : float; | |
swipeValue = Mathf.Sign(touch.position.y - startPos.y); | |
if (swipeValue > 0)//up | |
{ | |
//Jump (); | |
//Swipe.text = "Up Swipe"; | |
} | |
else if (swipeValue < 0)//down | |
{ | |
//Shrink (); | |
//Swipe.text = "Down Swipe"; | |
} | |
} | |
var swipeDistHorizontal : float; | |
swipeDistHorizontal = (new Vector3(touch.position.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude; | |
if (swipeDistHorizontal > minSwipeDistX) | |
{ | |
swipeValue = Mathf.Sign(touch.position.x - startPos.x); | |
if (swipeValue > 0)//right | |
{ | |
//MoveRight (); | |
//Swipe.text = "Right Swipe"; | |
} | |
else if (swipeValue < 0)//left | |
{ | |
//MoveLeft (); | |
//Swipe.text = "Left Swipe"; | |
} | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment