Anyone got a sliding finger (touch) gesture working and if so, wanna share some code?
Like a slide left/slide right gesture.
Anyone got a sliding finger (touch) gesture working and if so, wanna share some code?
Like a slide left/slide right gesture.
yes please.
i am very interesed in that too
lesfundi
This has already been covered before and should be on the unify site.
Here are some of my snippets
public int deviationAmount = 20;
public ArrayList trackers = new ArrayList();
Hashtable trackerLookup = new Hashtable();
private ArrayList ended;
private TouchTracker tracker;
private ArrayList movements;
....
void Update () {
// clean all touches (so they know if they aren't updated after we pull info)
for(i=0;i<trackers.Count;i++)
((TouchTracker)trackers[i]).Clean();
// track which events vanished (without using iPhoneTouchPhase.Ended)
ended = new ArrayList();
// process our touches
for(i = 0;i<iPhoneInput.touches.Length;i++)
{
// iPhoneTouchPhase.Ended isn't very reliable (especially with the remote)
// but this is how we can tell if we got single or double finger taps
if(iPhoneInput.touches[i].phase == iPhoneTouchPhase.Ended iPhoneInput.touches[i].tapCount>0)
{
if(iPhoneInput.touchCount == 1)
{
//Debug.Log("tracker.tapCount="+touch.tapCount);
this.HandleSingleTap();
return;
}
if(iPhoneInput.touchCount == 2 iPhoneInput.touches[i].fingerId == 1)
{
//Debug.Log("tracker.tapCount="+touch.tapCount);
this.HandleTwoFingerTap();
return;
}
}
else
{
// try to get our tracker for this finger id
tracker = (TouchTracker)trackerLookup[iPhoneInput.touches[i].fingerId];
if(tracker != null)
tracker.Update(iPhoneInput.touches[i]);
else
tracker = BeginTracking(iPhoneInput.touches[i]);
}
}
// use an intermediate list because EndTracking removes from trackers arraylist
for(i=0;i<trackers.Count;i++)
{
if(!((TouchTracker)trackers[i]).isDirty)
ended.Add(trackers[i]);
}
movements = new ArrayList();
for(i=0;i<ended.Count;i++)
{
movements.Add(EndTracking((TouchTracker)ended[i]));
}
if(movements.Count == 0)
return;
//Debug.Log("movements.Count="+movements.Count);
//single finger swipe and only do it if we have finished all trackers
if(movements.Count == 1 trackers.Count==0)
{
this.HandleSingleSwipe((Vector2)movements[0]);
}
//two finger swipe and only do it if we have finished all trackers
else if(movements.Count == 2 trackers.Count==0)
{
this.HandleTwoFingerSwipe((Vector2)movements[0],(Vector2)movements[1]);
}
}
....
void HandleSingleSwipe(Vector2 movement)
{
if(IsSwipeRight(movement))
{
//Debug.Log("Single Finger Swipe Right");
}
}
....
bool IsSwipeRight(Vector2 movement)
{
if(movement.x > deviationAmount Mathf.Abs(movement.y) < deviationAmount)
return true;
return false;
}
TouchTracker BeginTracking(iPhoneTouch touch)
{
TouchTracker tracker = new TouchTracker(touch);
// remember our tracker
trackers.Add(tracker);
trackerLookup[touch.fingerId] = tracker;
return tracker;
}
Vector2 EndTracking(TouchTracker tracker)
{
Vector2 movement = tracker.End();
trackers.Remove(tracker);
trackerLookup[tracker.fingerId] = null;
return movement;
}
Hi, here is a script I made today if you want to give it a try. Not massively tested. If you find any issues plz post I will take a look at it.
/*
Swipe gesture for iPhone. Add this script and a GUIText component to an empty GO
With the GO selected in the inspector set:
swipeLength // this is how long you want the swipe to be. 25 pixels seems ok
swipeVariance // this is how far the drag can go 'off line'. 5 pixels either way seems ok
You can swipe as many fingers left or right and it will only pick up one of them
It will then allow further swipes when that finger has been lifed from the screen.
Typically its swipe > lift finger > swipe .......
Be aware that it sometimes does not pick up the iPhoneTouchPhase.Ended
This is either a bug in the logic (plz test) or as the TouchPhases are notoriously
inaccurate its could well be this or it could be iPhone 1.6 given that it is quirky with touches.
Anyhow it does not affect the working of the class
other than a dead swipe once in a while which then rectifies itself on the next swipe so
no big deal.
No need for orientation as it will respect whatever you set.
*/
using UnityEngine;
using System.Collections;
public class Swipe : MonoBehaviour {
//public member vars
public int swipeLength;
public int swipeVariance;
//private member vars
private GUIText swipeText;
private Vector2[] fingerTrackArray;
private bool[] swipeCompleteArray;
private int activeTouch = -1;
//methods
void Start()
{
//get a reference to the GUIText component
swipeText = (GUIText) GetComponent(typeof(GUIText));
fingerTrackArray = new Vector2[5];
swipeCompleteArray = new bool[5];
}
void Update()
{
//touch count is a mess at the moment so add the extra check to see if there are no more than 5 touches
if(iPhoneInput.touchCount > 0 iPhoneInput.touchCount < 6)
{
foreach(iPhoneTouch touch in iPhoneInput.touches)
{
if(touch.phase == iPhoneTouchPhase.Began)
{
fingerTrackArray[touch.fingerId] = touch.position;
}
//check if withing swipe variance
if(touch.position.y > (fingerTrackArray[touch.fingerId].y + swipeVariance))
fingerTrackArray[touch.fingerId] = touch.position;
if(touch.position.y < (fingerTrackArray[touch.fingerId].y - swipeVariance))
fingerTrackArray[touch.fingerId] = touch.position;
//swipe right
if((touch.position.x > fingerTrackArray[touch.fingerId].x + swipeLength) !swipeCompleteArray[touch.fingerId]
activeTouch == -1)
{
activeTouch = touch.fingerId;
//Debug.Log(touch.fingerId + " " + fingerTrackArray[touch.fingerId] + " " + touch.position);
swipeCompleteArray[touch.fingerId] = true;
SwipeComplete("swipe right ", touch);
}
//swipe left
if((touch.position.x < fingerTrackArray[touch.fingerId].x - swipeLength) !swipeCompleteArray[touch.fingerId]
activeTouch == -1)
{
activeTouch = touch.fingerId;
//Debug.Log(touch.fingerId + " " + fingerTrackArray[touch.fingerId] + " " + touch.position);
swipeCompleteArray[touch.fingerId] = true;
SwipeComplete("swipe left ", touch);
}
//when the touch has ended we can start accepting swipes again
if(touch.fingerId == activeTouch touch.phase == iPhoneTouchPhase.Ended)
{
//Debug.Log("Ending " + touch.fingerId);
//if more than one finger has swiped then reset the other fingers so
//you do not get a double/triple etc. swipe
foreach(iPhoneTouch touchReset in iPhoneInput.touches)
{
fingerTrackArray[touchReset.fingerId] = touchReset.position;
}
swipeCompleteArray[touch.fingerId] = false;
activeTouch = -1;
}
}
}
}
void SwipeComplete(string messageToShow, iPhoneTouch touch)
{
swipeText.text = messageToShow;
//Debug.Log("doing something");
//Do something here
}
}
If you find it works ok plz let me know. Also if you want to enter a timer to check swipe against slow drag it should be pretty easy.
Take a look,thanks for share~~
Anyone have something in JS? Only seen C# so far, and I have no idea how to implement into my JS-based App.
You might be interested in checking out the FingerGestures scripting package from the Asset store. You can get more information in the forum thread.
i am using this code for a slide arrow to load the menu:
if (Input.GetTouch(i).phase == TouchPhase.Moved BackTouch)
{
backArrow.transform.position.x += 2;
if(touch.position.x > pressedLocation+Screen.width*0.1)
{
backArrow.transform.position.x = backArrowOriginalPos;
Start();
click.audio.Play();
Application.LoadLevel (0);
}
}
if (Input.GetTouch(i).phase == TouchPhase.Ended BackTouch)
{
backArrow.transform.position.x = backArrowOriginalPos;
BackTouch = false;
Here’s what I’ve been using, it’s CS but I used it in my JS project since the action is pretty simple it works fine. Not my own work but I edited some of it:
http://u3d.as/content/devesh-pandey/easy-gesture
Gestures: