- Home /
Swipe detection script help?
Hey guys I need help. I created this code to help detect swipe direction input, then spawn arrows if they swiped in the direction of the previous arrow.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AllTogether : MonoBehaviour {
private float fingerStartTime = 0.0f;
private Vector2 fingerStartPos = Vector2.zero;
private bool isSwipe = false;
private float minSwipeDist = 5.0f;
private float maxSwipeTime = 0.0f;
public List<GameObject> prefabList = new List<GameObject>();
public GameObject ArrowUp;
public GameObject ArrowDown;
public GameObject ArrowLeft;
public GameObject ArrowRight;
// Use this for initialization
void Start () {
Spawner ();
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0) {
foreach (Touch touch in Input.touches) {
switch (touch.phase) {
case TouchPhase.Began:
isSwipe = true;
fingerStartTime = Time.time;
fingerStartPos = touch.position;
break;
case TouchPhase.Canceled:
isSwipe = false;
break;
case TouchPhase.Ended:
float gestureTime = Time.time - fingerStartTime;
float gestureDist = (touch.position - fingerStartPos).magnitude;
if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist) {
Vector2 direction = touch.position - fingerStartPos;
Vector2 swipeType = Vector2.zero;
if (GameObject.Find ("Right") != null) {
if (swipeType.x != 0.0f) {
if (swipeType.x > 0.0f) {
Destroy (GameObject.FindWithTag ("Arrow"));
Spawner ();
}else {
GUI.Label (new Rect (0, 0, Screen.width, Screen.height), "You Lost");
}
}
if (GameObject.Find ("Left")!= null){
if (swipeType.x != 0.0f) {
if (swipeType.x > 0.0f) {
GUI.Label (new Rect (0, 0, Screen.width, Screen.height), "You Lost");
}else {
Destroy (GameObject.FindWithTag ("Arrow"));
Spawner ();
}
}
if (GameObject.Find ("Up") != null){
if (swipeType.y != 0.0f) {
if (swipeType.y > 0.0f) {
Destroy (GameObject.FindWithTag ("Arrow"));
Spawner ();
}else {
GUI.Label (new Rect (0, 0, Screen.width, Screen.height), "You Lost");
}
}
if (GameObject.Find ("Down") != null) {
if (swipeType.y != 0.0f) {
if (swipeType.y > 0.0f) {
GUI.Label (new Rect (0, 0, Screen.width, Screen.height), "You Lost");
}else{
Destroy (GameObject.FindWithTag ("Arrow"));
Spawner ();
}
}
}
}
}
}
}
break;
}
}
}
}
void Spawner(){
prefabList.Add(ArrowUp);
prefabList.Add(ArrowDown);
prefabList.Add(ArrowLeft);
prefabList.Add(ArrowRight);
int prefabIndex = UnityEngine.Random.Range(0,4);
Instantiate(prefabList[prefabIndex], new Vector3(0, 1, 10),Quaternion.Euler (0,0,0));// This one spawns it
}
}
But for some reason it doesn't work. It has no arrows, but it doesn't seem to take ANY input in when the game is built. I even added a Touch Input Module and a Standalone Input Module. Nothing seems to be working... Any help would be truly appreciated.
Answer by Mr-Men · Jun 02, 2015 at 05:58 AM
Firstly,
private float maxSwipeTime = 0.0f;
will ensure that
if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist)
always returns false because gestureTime is always greater than 0.0f. As a result, the conditional code block is never entered.
Secondly, the curly brackets after
if (GameObject.Find ("Right") != null)
closes at the end of the case condition. It should end before
if (GameObject.Find ("Left") != null)
I'd advise using some script editor, like MonoDevelop, to check the indentation and nesting of your code.
Your answer

Follow this Question
Related Questions
Help Ship Controll 0 Answers
Help for my thesis 2 Answers
works well On computer but not on phone,reads well on computer but not on phone 0 Answers
Detect swipe along 3d arrows 1 Answer
How can I change my mouse cursor every time I click? 1 Answer