- Home /
Question by
HaRoStudio · Jan 13, 2018 at 10:41 AM ·
touch controlsswipeunity android
Swipe reaction slow
hi guys,so im pretty new in unity and i was looking thru forums and i found this swipe script, i decided to implement it and it works fine but the time the object takes to move is very slow. is there any way i can change the reaction speed,thanks. using System.Collections; using System.Collections.Generic; using UnityEngine;
public enum Swipe { None, Up, Down, Left, Right };
public class swipe : MonoBehaviour
{
public float minSwipeLength = 5f;
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
Vector2 firstClickPos;
Vector2 secondClickPos;
public static Swipe swipeDirection;
void Update()
{
DetectSwipe();
}
public void DetectSwipe()
{
if (Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
firstPressPos = new Vector2(t.position.x, t.position.y);
}
if (t.phase == TouchPhase.Ended)
{
secondPressPos = new Vector2(t.position.x, t.position.y);
currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
// Make sure it was a legit swipe, not a tap
if (currentSwipe.magnitude < minSwipeLength)
{
swipeDirection = Swipe.None;
return;
}
currentSwipe.Normalize();
// Swipe up
if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
swipeDirection = Swipe.Up;
Vector3 position = this.transform.position;
position.y++;
this.transform.position = position;
// Swipe down
}
else if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
swipeDirection = Swipe.Down;
Vector3 position = this.transform.position;
position.y--;
this.transform.position = position;
// Swipe left
}
else if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
{
swipeDirection = Swipe.Left;
Vector3 position = this.transform.position;
position.x--;
this.transform.position = position;
// Swipe right
}
else if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
{
swipeDirection = Swipe.Right;
Vector3 position = this.transform.position;
position.x++;
this.transform.position = position;
}
}
}
else
{
if (Input.GetMouseButtonDown(0))
{
firstClickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
else
{
swipeDirection = Swipe.None;
//Debug.Log ("None");
}
if (Input.GetMouseButtonUp(0))
{
secondClickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
currentSwipe = new Vector3(secondClickPos.x - firstClickPos.x, secondClickPos.y - firstClickPos.y);
// Make sure it was a legit swipe, not a tap
if (currentSwipe.magnitude < minSwipeLength)
{
swipeDirection = Swipe.None;
return;
}
currentSwipe.Normalize();
//Swipe directional check
// Swipe up
if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
swipeDirection = Swipe.Up;
// Swipe down
}
else if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
{
swipeDirection = Swipe.Down;
Debug.Log("down");
// Swipe left
}
else if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
{
swipeDirection = Swipe.Left;
Debug.Log("Left");
Vector3 position = this.transform.position;
position.x--;
this.transform.position = position;
// Swipe right
}
else if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
{
swipeDirection = Swipe.Right;
Debug.Log("right");
}
}
}
}
}
Comment