- Home /
simple android swipe controls for level selection?
So I am having serious troubles getting any form of swiping to be detected. All I want to do is transform an objects x value +-1. This is for a menu that would look like most polular mobile level selection menus. Is there an easy way to do this? If so, where should I start? I have already tried the quick swipe script and can't get it to work.
Answer by GamingNewBie · Feb 17, 2013 at 07:19 AM
Drag an empty Game object to the scene attach the following script to it. I hope this will help.
using UnityEngine; using System.Collections;
public class SwipeTest : MonoBehaviour {
Vector2 StartPos;
int SwipeID = -1;
float minMovement = 20.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
foreach (var T in Input.touches) {
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1) {
SwipeID = T.fingerId;
StartPos = P;
} else if (T.fingerId == SwipeID) {
var delta = P - StartPos;
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement) {
SwipeID = -1;
if (Mathf.Abs (delta.x) > Mathf.Abs (delta.y)) {
if (delta.x > 0) {
Debug.Log ("Swipe Right Found");
} else {
Debug.Log ("Swipe Left Found");
}
}
else {
if (delta.y > 0) {
Debug.Log ("Swipe Up Found");
} else {
Debug.Log ("Swipe Down Found");
}
}
} else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
}
}
}
Thanks! It's working! Do you think you could help me convert this into javascript? I tried but it didn't work. $$anonymous$$y whole project is in javascript and I need to reference things from this script and that just complicates things. Here's my failed attempt.
var SwipeID : int = -1;
var $$anonymous$$$$anonymous$$ovement : float = 20.0f;
public var gridline : GameObject;
var temp : Vector3;
var swiping : boolean = false;
var whichdot : int = 1;
var dot1 : GameObject;
var dot2 : GameObject;
var dot3 : GameObject;
var dot4 : GameObject;
function Update ()
{
(whichdot == 1)
{
dot1.GetComponent(GUITexture).enabled = true;
dot2.GetComponent(GUITexture).enabled = false;
dot3.GetComponent(GUITexture).enabled = false;
dot4.GetComponent(GUITexture).enabled = false;
}
if(whichdot == 2)
{
dot2.GetComponent(GUITexture).enabled = true;
dot1.GetComponent(GUITexture).enabled = false;
dot3.GetComponent(GUITexture).enabled = false;
dot4.GetComponent(GUITexture).enabled = false;
}
if(whichdot == 3)
{
dot3.GetComponent(GUITexture).enabled = true;
dot2.GetComponent(GUITexture).enabled = false;
dot1.GetComponent(GUITexture).enabled = false;
dot4.GetComponent(GUITexture).enabled = false;
}
if(whichdot == 4)
{
dot4.GetComponent(GUITexture).enabled = true;
dot2.GetComponent(GUITexture).enabled = false;
dot3.GetComponent(GUITexture).enabled = false;
dot1.GetComponent(GUITexture).enabled = false;
}
temp = gridline.transform.position;
for (var T in Input.touches) {
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1) {
SwipeID = T.fingerId;
StartPos = P;
} else if (T.fingerId == SwipeID) {
var delta = P - StartPos;
if (T.phase == TouchPhase.$$anonymous$$oved && delta.magnitude > $$anonymous$$$$anonymous$$ovement) {
SwipeID = -1;
if ($$anonymous$$athf.Abs (delta.x) > $$anonymous$$athf.Abs (delta.y)) {
if (delta.x > 0) {
//swipe right
if(temp.x <= 60.28 && temp.x >= 56.28)
{
swiping = true;
temp.x = temp.x + 1;
gridline.transform.position = temp;
whichdot = whichdot -1;
waitalittle();
}
} else {
//swipe left
if(temp.x <= 60.28 && temp.x >= 56.28)
{
swiping = true;
temp.x = temp.x - 1;
gridline.transform.position = temp;
whichdot = whichdot +1;
waitalittle();
}
}
}
else {
if (delta.y > 0) {
Debug.Log ("Swipe Up Found");
} else {
Debug.Log ("Swipe Down Found");
}
}
} else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
}
}
function waitalittle()
{
yield WaitForSeconds (1);
swiping = false;
}
}
Your answer
Follow this Question
Related Questions
Android Button Text 1 Answer
Changing level on Android 3 Answers
Sliding Finger(touch) gesture? 1 Answer
Language menu problem 1 Answer
pause menu on android phone 1 Answer