Hi, I need to know how to find out if the user has used the swipe gesture. I don't want the swipe gesture to move anything, calculatge velocity i just need the code to know if the user has used the gesture. I use c#. Code (csharp): public void hasUsedSwipe(){ if(usedSwipe){ //do something } } Basically thats all i need. I just dont no how to check if the swipe has occurred. Thanks
look up input.gettouch http://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html then use the first example Code (csharp): // Moves object according to finger movement on the screen var speed : float = 0.1; function Update () { if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Moved) { // Get movement of the finger since last frame var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition; // Move object across XY plane transform.Translate (-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0); } } now where it says Code (csharp): // Move object across XY plane transform.Translate (-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0); change that to say if the touchDeltaPosition.x is greater then whatever distance you want the user to need to move, the user swyped left or right (use Y for up and down) Code (csharp): if(touchDeltaPosition.x >1) { SwypeRight(); }
What I do to detect swipes is get the initial point of the touch and save it, then on touch move I compare the initial point with the new point and if the distance between them (sqrmagnitude distance... faster then Vector3.distance) is > then the desired input move. On this matter what I would love to know is if there is some way to detect complex touches... for example... detect if the user made a circle move... a square move... etc, but I dont think that is very simple. Best regards, Allan
The cited methods for performing a swipe gesture I've found woefully inadequate for my particular scenario. So I've written a better method which I will provide here; How this works is very simple, a little bit of pythag and a little trig to work out the distance the finger has travelled and the angle at which it has travelled. If the speed, distance and angle are within our particular requirements we react. This is easily customisable code but as the other examples above didn't fit I threw this together fairly quickly to produce reasonable results. Code (csharp): using UnityEngine; using System.Collections; public class SwipeGesture : MonoBehaviour { Vector2 startPosition; float startTime; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Ended) { Vector2 endPosition = Input.GetTouch(0).position; Vector2 delta = endPosition - startPosition; float dist = Mathf.Sqrt(Mathf.Pow(delta.x, 2) + Mathf.Pow (delta.y, 2)); float angle = Mathf.Atan (delta.y/delta.x) * (180.0f/Mathf.PI); float duration = Time.time - startTime; float speed = dist/duration; // Left to right swipe if (startPosition.y < endPosition.y) { if (angle < 0) angle = angle * 1.0f; Debug.Log ("Distance: " + dist + " Angle: " + angle + " Speed: " + speed); if (dist > 300 angle < 10 speed > 1000) { // Do something related to the swipe } } } if (Input.touchCount > 0 Input.GetTouch (0).phase == TouchPhase.Began) { startPosition = Input.GetTouch(0).position; startTime = Time.time; } } } Hope this helps.
Code (csharp): if (angle < 0) angle = angle * 1.0f; Did you mean...? Code (csharp): if (angle < 0) angle = angle * -1.0f;
Here is a simple approach to detect a swipe in LTR (left to right) and RTL (right to left) directions: Code (CSharp): private Vector3 startPosition = Vector3.zero; private Vector3 endPosition = Vector3.zero; void Update() { if (Input.GetMouseButtonDown(0)) // swipe begins { startPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); } if (Input.GetMouseButtonUp(0)) // swipe ends { endPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); } if (startPosition != endPosition && startPosition != Vector3.zero && endPosition != Vector3.zero) { floatdeltaX = endPosition.x - startPosition.x; floatdeltaY = endPosition.y - startPosition.y; if ((deltaX > 5.0f || deltaX < -5.0f) && (deltaY >= -1.0f || deltaY <= 1.0f)) { if (startPosition.x < endPosition.x) // swipe LTR { print("LTR"); } else // swipe RTL { print("RTL"); } } startPosition = endPosition = Vector3.zero; } } Hope this helps!
I searched for the same thing, and found it reasonable to create asset for easy swipe detection, and share it with community. So here it is on github. My solution supports different usecases, including: 8-directions swipe detection, 4-directions, 2-directions (left-right or up-down), swipes on hexagonal grid. All listed is included as a presets, but also you can configure it to detect any number of Vector3 directions. So ti`s really flexible. Also, you can try WebGL build or see video tutorial. If you try it, please, let me know (via youtube comment, or see Contacts section on github), was it suitable for your case, and was it comfortable enough.