I’ve nearly finished the live training on mobile development. I don’t think he covered this but how would you check whether someone has swiped up in the game?
I have no clue where to start!
I’ve nearly finished the live training on mobile development. I don’t think he covered this but how would you check whether someone has swiped up in the game?
I have no clue where to start!
When using the following script, play around with the minimum swipe distances for the best results (may vary on some mobile devices)… For me I found that 128 worked best for minSwipeDistY and 256 worked best for minSwipeDistX
using UnityEngine;
using System.Collections;
public class SwipeDetector : MonoBehaviour
{
public float minSwipeDistY;
public float minSwipeDistX;
private Vector2 startPos;
public GUIStyle _responseStyle;
public string _swipe;
void Update()
{
#if UNITY_ANDROID
if (Input.touchCount > 0){
Touch touch = Input.touches[0];
switch (touch.phase){
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Ended:
float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistVertical > minSwipeDistY){
float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
if (swipeValue > 0){ //up swipe
_swipe = "Up";
} else if (swipeValue < 0){ //down swipe
_swipe = "Down";
}
}
float swipeDistHorizontal = (new Vector3(touch.position.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0){ //right swipe
_swipe = "Right";
} else if (swipeValue < 0){ //left swipe
_swipe = "Left";
}
}
break;
}
}
}
#endif
void OnGUI ()
{
GUI.Label (new Rect(10, Screen.height - 50, 100, 20), gameObject.name + ".SwipeDetector._swipe = " + _swipe, _responseStyle);
}
}