Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Tutorial: Detect swipe direction simple and easy, no need to use update

Discussion in 'Scripting' started by Max_power1965, May 10, 2017.

  1. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    Hi guys, I wrote this simple tutorial wich is basically 7 lines of code and works like a charm. Easy and simple. You just have to use te build in Unity event system instead to write long and useless code.
    No need to use an update or fixed update.
    Here a snipped of the code:

    Code (CSharp):
    1. public void OnEndDrag(PointerEventData eventData)
    2. {
    3. Vector3 dragVectorDirection = (eventData.position - eventData.pressPosition).normalized;
    4. GetDragDirection(dragVectorDirection);
    5. }
    Code (CSharp):
    1. private enum DraggedDirection
    2. {
    3.    Up,
    4.    Down,
    5.    Right,
    6.    Left
    7. }
    8. private DraggedDirection GetDragDirection(Vector3 dragVector)
    9. {
    10.   float positiveX = Mathf.Abs(dragVector.x);
    11.   float positiveY = Mathf.Abs(dragVector.y);
    12.   DraggedDirection draggedDir;
    13.   if (positiveX > positiveY)
    14.   {
    15.     draggedDir = (dragVector.x > 0) ? DraggedDirection.Right : DraggedDirection.Left;
    16.   }
    17.   else
    18.   {
    19.     draggedDir = (dragVector.y > 0) ? DraggedDirection.Up : DraggedDirection.Down;
    20.   }
    21.     Debug.Log(draggedDir);
    22.     return draggedDir;
    23. }
    As you can see is very easy. Uf you want to know more you can download the full source code in the article.
    The main advantages of this system is that It'll work in both Mobile and non-mobile platforms
     
    jojue, codxr, akash_virodhia and 2 others like this.
  2. PadraigCrowley

    PadraigCrowley

    Joined:
    Aug 24, 2018
    Posts:
    2
    Hi Max, the download link in the article no longer seems to work.
    Anywhere else I can download it from?
    Thanks.
     
    calcuff1 likes this.
  3. Daniel_Abeleira

    Daniel_Abeleira

    Joined:
    Oct 16, 2018
    Posts:
    7
    This doesn't work for mobile, does it?
     
  4. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    Yes absolutely! Why not?
     
  5. Daniel_Abeleira

    Daniel_Abeleira

    Joined:
    Oct 16, 2018
    Posts:
    7
    Ok good to know. Just asking.
     
  6. gaglabs

    gaglabs

    Joined:
    Oct 17, 2019
    Posts:
    185
    So how would you call this?
     
  7. SiWoC

    SiWoC

    Joined:
    Feb 16, 2021
    Posts:
    9
    Add a script Component with the above code (both parts)
    Make sure your script has:
    Code (CSharp):
    1.  
    2.     public class PageSwiper : MonoBehaviour, IDragHandler, IEndDragHandler{
    3.       ...
    4.  
     
    Last edited: Apr 8, 2021
    jojue likes this.
  8. bdutton12

    bdutton12

    Joined:
    Apr 11, 2019
    Posts:
    7
    I am getting error that those namespaces can't be found, do I need any new "using ..."?
     
  9. SiWoC

    SiWoC

    Joined:
    Feb 16, 2021
    Posts:
    9
    yes, you need "using UnityEngine.EventSystems;"
    Microsoft Visual Studio tells you that doesn't it?
    And for this example you probably only need IEndDragHandler since he only has an OnEndDrag in his code.
     
  10. cester

    cester

    Joined:
    Feb 26, 2014
    Posts:
    1
    Legend! Works perfect.!
     
  11. viettungvuongedu

    viettungvuongedu

    Joined:
    Oct 7, 2021
    Posts:
    1
    The code doesn't work on my machine. Am I missing something?
     
  12. wayneforce

    wayneforce

    Joined:
    Feb 12, 2014
    Posts:
    41
    This was great. For new comers: you need to add a panel and attach the script to it in the hierarchy. You also need an event system component in the hierarchy, for it to work.
     
  13. standardcombo

    standardcombo

    Joined:
    Jun 28, 2012
    Posts:
    19
    Wouldn't it be better as a Vector2?