Created
May 10, 2013 12:08
-
-
Save g0ody/5554017 to your computer and use it in GitHub Desktop.
Script for Swipe detection, uses Playmaker to Fire Events
This file contains hidden or 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; | |
using HutongGames.PlayMaker; | |
public class ControllerSwipe : MonoBehaviour { | |
public float comfortZone = 70.0f; | |
public float minSwipeDist = 14.0f; | |
public float maxSwipeTime = 0.5f; | |
public float fastSwipeReaction = 15f; | |
public FsmEvent right; | |
public FsmEvent left; | |
public FsmEvent down; | |
public FsmEvent up; | |
private float swipeTimeStart; | |
private Vector2 swipePosStart; | |
private bool couldBeSwipe; | |
private bool swipeDone; | |
public float acceleromterZone = 0.5f; | |
private Vector3 initAccelerometer; | |
public enum SwipeDirection { | |
None, | |
Up, | |
Down, | |
Right, | |
Left | |
} | |
public SwipeDirection swipeType = SwipeDirection.None; | |
void Update() | |
{ | |
UpdateSwipeAction(); | |
} | |
private void UpdateSwipeAction() | |
{ | |
if (Input.touchCount > 0) | |
{ | |
Touch touch = Input.touches[0]; | |
switch (touch.phase) | |
{ | |
case TouchPhase.Began: | |
swipeType = SwipeDirection.None; | |
couldBeSwipe = true; | |
swipeDone = false; | |
swipePosStart = touch.position; | |
swipeTimeStart = Time.time; | |
break; | |
case TouchPhase.Moved: | |
var offset = touch.position - swipePosStart; | |
switch (swipeType) | |
{ | |
case SwipeDirection.None: | |
var isVertical = Mathf.Abs(offset.y) > Mathf.Abs(offset.x); | |
if (offset.x < -minSwipeDist && !isVertical) | |
swipeType = SwipeDirection.Left; | |
else if (offset.x > minSwipeDist && !isVertical) | |
swipeType = SwipeDirection.Right; | |
else if (offset.y > minSwipeDist && isVertical) | |
swipeType = SwipeDirection.Up; | |
else if (offset.y < -minSwipeDist && isVertical) | |
swipeType = SwipeDirection.Down; | |
break; | |
case SwipeDirection.Left: | |
case SwipeDirection.Right: | |
if (Mathf.Abs(offset.y) > comfortZone) | |
couldBeSwipe = false; | |
break; | |
case SwipeDirection.Up: | |
case SwipeDirection.Down: | |
if (Mathf.Abs(offset.x) > comfortZone) | |
couldBeSwipe = false; | |
break; | |
} | |
if (offset.magnitude > fastSwipeReaction) | |
DoSwipeAction(); | |
break; | |
case TouchPhase.Ended: | |
DoSwipeAction(); | |
break; | |
} | |
} | |
} | |
private void DoSwipeAction() | |
{ | |
if (couldBeSwipe && !swipeDone && Time.time - swipeTimeStart < maxSwipeTime) | |
{ | |
switch (swipeType) | |
{ | |
case SwipeDirection.Left: PlayMakerFSM.BroadcastEvent(left.Name); break; | |
case SwipeDirection.Right: PlayMakerFSM.BroadcastEvent(right.Name); break; | |
case SwipeDirection.Down: PlayMakerFSM.BroadcastEvent(down.Name); break; | |
case SwipeDirection.Up: PlayMakerFSM.BroadcastEvent(up.Name); break; | |
} | |
swipeDone = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment