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 D3ntrax · Aug 19, 2014 at 09:55 PM · touchswipe

Swipe Swipe is Laggy

Hello There,

I'm trying to script a basic mobile swipe controller as the user swipe the screen. I've almost got it working, but it's laggy. When i am testing on keyboard contoller class, it's no laggy. (~1-10 ms.) But when i package unity game as .APK file and install to my LG G2 mobile phone, owever when swiping screen the anywhere, command is sending with delay. (~100-300 ms.)

Any ideas ?

Here is my codes;

Swipe dedector.cs

 using UnityEngine;
 using System.Collections;
 
 public enum SwipeDirection {
     None,
     Up,
     Down,
     Left,
     Right
 }
 
 public class SwipeDetector : MonoBehaviour {
 
     public float comfortZone = 500.0f;
     public float minSwipeDist = 25f;
     public float maxSwipeTime = 0.5f;
 
     private Vector2 startPos;
     private bool couldBeSwipe;
     private float startTime;
     
     public SwipeDirection swipeDirection;
     public float lastSwipeTime;
 
     void Update()
     {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.touches[0];
             
             switch (touch.phase)
             {
             case TouchPhase.Began:
                 swipeDirection = SwipeDirection.None;
                 lastSwipeTime = 0;
                 couldBeSwipe = true;
                 startPos = touch.position;
                 startTime = Time.time;
                 break;        
             case TouchPhase.Moved:
                 if (Mathf.Abs(touch.position.x - startPos.x) > comfortZone)
                 {
                     Debug.Log("Not a swipe. Swipe strayed " + (int)Mathf.Abs(touch.position.x - startPos.x) +
                               "px which is " + (int)(Mathf.Abs(touch.position.x - startPos.x) - comfortZone) +
                               "px outside the comfort zone.");
                     couldBeSwipe = false;
                 }
                 break;
             case TouchPhase.Ended:
                 if (couldBeSwipe)
                 {
                     float swipeTime = Time.time - startTime;
                     float swipeDistHorizontal = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
                     float swipeDistVertical = (new Vector3(touch.position.x, 0, 0) - new Vector3( startPos.x, 0, 0)).magnitude;
                     float swipeValueHorizontal = Mathf.Sign(touch.position.y - startPos.y);
                     float swipeValueVertical = Mathf.Sign(touch.position.x - startPos.x);
                     if (swipeDistHorizontal > swipeDistVertical)
                     {
                         if (swipeDistHorizontal > minSwipeDist && swipeTime < maxSwipeTime)
                         {
                             if (swipeValueHorizontal > 0)
                                 swipeDirection = SwipeDirection.Up;
                             else if (swipeValueHorizontal < 0)
                                 swipeDirection = SwipeDirection.Down;
                         }
                         lastSwipeTime = Time.time;
                     }
 
                     if (swipeDistHorizontal < swipeDistVertical)
                     {
                         if (swipeDistVertical > minSwipeDist && swipeTime < maxSwipeTime)
                         {
                             if (swipeValueVertical > 0)
                                 swipeDirection = SwipeDirection.Right;
                             else if (swipeValueVertical < 0)
                                 swipeDirection = SwipeDirection.Left;
 
                             lastSwipeTime = Time.time;
                         }
                     }
                 }
                 break;
             }
         }
     }
 }


SwipeController Tile.cs

 public void SwipeControl() 
     {        
         SwipeDetector swiper = GameObject.Find ("SwipeDedector").GetComponent<SwipeDetector> ();
 
         if (swiper.swipeDirection == SwipeDirection.Up) 
         {
             GameObject.Find("_GameManager").GetComponent<GameManager>().ReverseBackgroundSprite();
             audioManager.ChangeBGAudio();
             swiper.swipeDirection = SwipeDirection.None;
         }
         if (swiper.swipeDirection == SwipeDirection.Down) 
         {
             if (!isRotating)
             {
                 state = State.Rotating;
                 isRotating = true;
                 audioManager.ChangeTileAudio();
             }
             swiper.swipeDirection = SwipeDirection.None;
         }
         if (swiper.swipeDirection == SwipeDirection.Left) 
         {
             audioManager.SwipeTileAudio();
             SetMovingLeft();
             state = State.CheckingMatches;
             swiper.swipeDirection = SwipeDirection.None;
         }
         if (swiper.swipeDirection == SwipeDirection.Right) 
         {
             audioManager.SwipeTileAudio();
             SetMovingRight();
             state = State.CheckingMatches;
             swiper.swipeDirection = SwipeDirection.None;
         }
     }

Tile.cs Update & FixedUpdate

     void Update () 
     {
         if (gameObject == null) 
         {
             Debug.LogError("Couldn't find an gameobject with name TILE");
             return;        
         }
         if (state == State.Loaded) {
             state = State.WaitingForInput;
         } else if (state == State.GameOver) {
 
         } else if (state == State.WaitingForInput) {
             InputControl();
             SwipeControl();
         } else if (state == State.CheckingMatches) {
             if (isMovingLeft)
             {
                 if (tilePosition == TilePosition.Center) 
                 {
                     
                 }
                 else if (tilePosition == TilePosition.Right)
                 {
                 
                 }
                 else if (tilePosition == TilePosition.Left)
                 {
                 
                 }
             }
             else if (isMovingRight)
             {
                 if (tilePosition == TilePosition.Center) 
                 {        
                 
                 }
                 else if (tilePosition == TilePosition.Right)
                 {
                 
                 }
                 else if (tilePosition == TilePosition.Left)
                 {
                     
                 }
             }
         } else if (state == State.Rotating) {
             if (isRotating )
             {
                 
             }
         }
     }
 
     void FixedUpdate()
     {
 
         transform.Translate(Vector3.up * Time.deltaTime * ((3*100)+MoveSpeed) / 100, Space.World);
     }




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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

detect Swipe in four directions (android) 7 Answers

How draw a path using touch in c# 1 Answer

A node in a childnode? 1 Answer

Controlling Animation ON TOUCH 0 Answers

How to play sound on GUI touch IOS 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