Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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:
    74
    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
     
  2. PadraigCrowley

    PadraigCrowley

    Joined:
    Aug 24, 2018
    Posts:
    1
    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:
    74
    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:
    5
    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
unityunity