[java]

using UnityEngine;

public class Swipe : MonoBehaviour
{
private Vector2 fingerDown;
private Vector2 fingerUp;
public bool detectSwipeOnlyAfterRelease = false;

public float SWIPE_THRESHOLD = 20f;
public GameObject gameObeject;
public float moveSpeed = 200f;

void Start(){

//Rigidbody rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{

foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerUp = touch.position;
fingerDown = touch.position;
}

//Detects Swipe while finger is still moving
if (touch.phase == TouchPhase.Moved)
{
if (!detectSwipeOnlyAfterRelease)
{
fingerDown = touch.position;
checkSwipe();
}
}

//Detects swipe after finger is released
if (touch.phase == TouchPhase.Ended)
{
fingerDown = touch.position;
checkSwipe();
}
}
}

void checkSwipe()
{
//Check if Vertical swipe
if (verticalMove() > SWIPE_THRESHOLD && verticalMove() > horizontalValMove())
{
//Debug.Log("Vertical");
if (fingerDown.y – fingerUp.y > 0)//up swipe
{
OnSwipeUp();
}
else if (fingerDown.y – fingerUp.y < 0)//Down swipe { OnSwipeDown(); } fingerUp = fingerDown; } //Check if Horizontal swipe else if (horizontalValMove() > SWIPE_THRESHOLD && horizontalValMove() > verticalMove())
{
//Debug.Log("Horizontal");
if (fingerDown.x – fingerUp.x > 0)//Right swipe
{
OnSwipeRight();
}
else if (fingerDown.x – fingerUp.x < 0)//Left swipe
{
OnSwipeLeft();
}
fingerUp = fingerDown;
}

//No Movement at-all
else
{
//Debug.Log("No Swipe!");
}
}

float verticalMove()
{
return Mathf.Abs(fingerDown.y – fingerUp.y);
}

float horizontalValMove()
{
return Mathf.Abs(fingerDown.x – fingerUp.x);
}

//////////////////////////////////CALLBACK FUNCTIONS/////////////////////////////
void OnSwipeUp()
{
Debug.Log("Swipe UP");
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}

void OnSwipeDown()
{
Debug.Log("Swipe Down");
transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
}

void OnSwipeLeft()
{
Debug.Log("Swipe Left");
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}

void OnSwipeRight()
{
Debug.Log("Swipe Right");
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
}
}
[/java]

Download Project

Leave a Reply

Your email address will not be published. Required fields are marked *