- Home /
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
Your answer
Follow this Question
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