Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jaffy25 · May 20, 2015 at 10:22 AM · inputswipe

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges