0% found this document useful (0 votes)
42 views4 pages

Pfonseca: Swipe Detection On Unity

This document describes a C# script for detecting swipes on mobile devices using Unity. The script uses TouchPhase events to detect when a touch begins, is canceled, or ends. It calculates the gesture time and distance to determine if it was a valid swipe gesture. The direction of the swipe is determined by comparing horizontal and vertical movement. If a swipe is detected, the appropriate direction (left, right, up, down) is returned as a Vector2.

Uploaded by

AbdulRofik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Pfonseca: Swipe Detection On Unity

This document describes a C# script for detecting swipes on mobile devices using Unity. The script uses TouchPhase events to detect when a touch begins, is canceled, or ends. It calculates the gesture time and distance to determine if it was a valid swipe gesture. The direction of the swipe is determined by comparing horizontal and vertical movement. If a swipe is detected, the appropriate direction (left, right, up, down) is returned as a Vector2.

Uploaded by

AbdulRofik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PFonseca

Developer
View site on GitHub Statuses Archives

Swipe detection on Unity


This is a simple class to use swipe on mobile devices with Unity.
using UnityEngine;
using System.Collections;
public class SwipeScript : MonoBehaviour {
private float fingerStartTime = 0.0f;
private Vector2 fingerStartPos = Vector2.zero;
private bool isSwipe = false;
private float minSwipeDist = 50.0f;
private float maxSwipeTime = 0.5f;
// Update is called once per frame
void Update () {
if (Input.touchCount > 0){
foreach (Touch touch in Input.touches)
{
switch (touch.phase)
{
case TouchPhase.Began :
/* this is a new touch */
isSwipe = true;
fingerStartTime = Time.time;
fingerStartPos = touch.position;
break;
case TouchPhase.Canceled :
/* The touch is being 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 (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)){
// the swipe is horizontal:
swipeType = Vector2.right * Mathf.Sign(direction.x);
}else{
// the swipe is vertical:
swipeType = Vector2.up * Mathf.Sign(direction.y);
}
if(swipeType.x != 0.0f){
if(swipeType.x > 0.0f){
// MOVE RIGHT
}else{
// MOVE LEFT
}
}
if(swipeType.y != 0.0f ){
if(swipeType.y > 0.0f){
// MOVE UP
}else{
// MOVE DOWN
}
converted by W eb2PDFConvert.com

}
}
break;
}
}
}
}
}
Published on 07/01/2015 at 02h20 by pfonseca, tags Unity

If you liked this article you can follow me on Twitter

6 comments
by Rahul Singh 19/10/2015 at 15h31
Awesome script..Thanks a lot.
by Logan Branjord 14/07/2015 at 20h58
This script worked great for me. Thanks for providing this. Ive used at least 10 other swipe scripts and I use this as my base now instead of any of
those.
by Num_T 14/06/2015 at 20h50
I may be getting slightly confused here. This code has been a great start for me and is working more or less as I expect.
However, I want to go on to perform more advanced stuff with it such as treating touches differently by determining whether they meet my criteria
for a swipe or not and I am a little concerned about the fact that there is of course an array of touches.
Shouldnt each touch have its own start time / end time, start position / end position? In theory are they overwriting each other at the moment? So if
you started a new touch at a critical moment say a frame before one was about to end it would reset the start time counter for example and what
should register as a swipe on the older touch would not?
If so I would probably go about setting up my own touch object inheriting from the Touch one and use instances with their own start end variables
etc. I was just wondering if I am right in thinking there is a potential problem as is or am just getting confused?!
Cheers
by sooon 06/05/2015 at 01h40
Hi, Thanks for the script. Work like a charm. Just curious, would it be better if using FixedUpdate? According to the document it update more
frequent than Update.
by PFonseca 07/04/2015 at 22h48
Ill try to create a double tap script.
by Gerick 19/03/2015 at 03h20
great!, thanks a lot
any idea for tap or double tap?

comment Swipe detection on Unity


Your name

converted by W eb2PDFConvert.com

Email address
Your website

comment
Trackbacks are disabled

Search
Search

Tags
Android C# cocos2d cocos2d-x

Events iOS
Git

iPhone5s MacOSX Rails Safari

Swift Unity

Windows Windows Phone x

Syndicate
Articles
Comments
architect-theme is maintained by jasonlong.
This page was generated by GitHub Pages using the Architect theme by Jason Long.
Powered by Publify
converted by W eb2PDFConvert.com

converted by W eb2PDFConvert.com

You might also like