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
              }
            }

          }
            
          break;
        }
      }
    }

  }
}

Published on 07/01/2015 at 02h20 by pfonseca, tags

If you liked this article you can follow me on Twitter
  • 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. I’ve 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.

    Shouldn’t each touch have it’s 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

    I’ll 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

Trackbacks are disabled

Powered by Publify