Created
May 13, 2013 23:21
-
-
Save shinriyo/5572375 to your computer and use it in GitHub Desktop.
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
using UnityEngine; | |
using System.Collections; | |
public class SwipeManager : MonoBehaviour | |
{ | |
private float startTime; | |
private Vector2 startPos; | |
private bool couldBeSwipe; | |
public float comfortZone; | |
public float minSwipeDist; | |
public float maxSwipeTime; | |
void Update () | |
{ | |
if (iPhoneInput.touchCount > 0) { | |
iPhoneTouch touch = iPhoneInput.touches [0]; | |
switch (touch.phase) { | |
case iPhoneTouchPhase.Began: | |
couldBeSwipe = true; | |
startPos = touch.position; | |
startTime = Time.time; | |
break; | |
case iPhoneTouchPhase.Moved: | |
if (Mathf.Abs (touch.position.y - startPos.y) > comfortZone) { | |
couldBeSwipe = false; | |
} | |
break; | |
case iPhoneTouchPhase.Stationary: | |
couldBeSwipe = false; | |
break; | |
case iPhoneTouchPhase.Ended: | |
float swipeTime = Time.time - startTime; | |
float swipeDist = (touch.position - startPos).magnitude; | |
if (couldBeSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist)) { | |
// It's a swiiiiiiiiiiiipe! | |
float swipeDirection = Mathf.Sign (touch.position.y - startPos.y); | |
// Do something here in reaction to the swipe. | |
Debug.Log (swipeDirection); | |
} | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment