- Home /
How to check whether you have swiped upwards on a mobile device - c#
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!
In the live training, I don't believe you swipe up, but rather you tap. He explains it in the tutorial. And using get button down, the longer you tap, the higher the character goes. For sliding, maybe you could make an invisible slider that resets everytime you release it, and you have to make the slider go higher for a higher jump??? Otherwise, you just tap.
$$anonymous$$aybe Dapheono's answer is all you need. But, in general, try to think of a similar problem other people must have. In this case, Search for how to check for a swipe (in any direction.) For sure there's sideways swipe examples you can change to go up/down.
It also depends on what else can happen. If all you can do is swipe, that's one thing. If you have a tappable button, and want to be able to have a small jiggle count as a tap, and only a big move be a swipe, thats different.
Answer by Daphoeno · Jul 08, 2015 at 06:44 PM
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);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
TouchInput doesn't not exist in current content 0 Answers
Android app is laggy 1 Answer
Detect Gyroscope's rotation around a single axis? Like a car game. 0 Answers