Search Unity

[HELP] Swipe movement

Discussion in 'Scripting' started by xxtickxtockxx, Jan 15, 2019.

  1. xxtickxtockxx

    xxtickxtockxx

    Joined:
    Jan 10, 2019
    Posts:
    16
    Hello, I have an issue with my swipe detection system it's a bit slow-Laggy since its only works on myTouch.phase == TouchPhase.Ended
    I tried working with on TouchPhase.Moved but then the object moves rapidly when I use it.

    I mainly want to detect if I moved my finger x so it swipes.

    Sorry for my English.


    Code (CSharp):
    1.     if (Input.touchCount > 0)
    2.             {
    3.                 Touch myTouch = Input.touches[0];
    4.    
    5.                 //Check if the phase of that touch equals Began
    6.                 if (myTouch.phase == TouchPhase.Began)
    7.                 {
    8.                     touchStart = myTouch.position;
    9.                     touchEnd = myTouch.position;
    10.                 }
    11.                 if (myTouch.phase == TouchPhase.Moved)
    12.                 {
    13.                     touchEnd = myTouch.position;
    14.    
    15.    
    16.                 }
    17.                 if (myTouch.phase == TouchPhase.Ended || Mathf.Abs(Vector3.Distance(touchEnd, touchEnd)) > touchDistance)
    18.                 {
    19.                     touchEnd = myTouch.position;
    20.                     float x = touchEnd.x - touchStart.x;
    21.                     float y = touchEnd.y - touchStart.y;
    22.    
    23.                     if (Mathf.Abs(x) > Mathf.Abs(y))
    24.                     {
    25.                         if (x > 0)
    26.                             MoveR();
    27.                         else
    28.                             MoveL();
    29.                     }
    30.                     else
    31.                     {
    32.                         if (y > 0)
    33.                             MoveU();
    34.                         else
    35.                             MoveD();
    36.                     }
    37.                 }
    38.             }
    39.    
    40.  

    ^ look at this video he swipes than the rocket moves I want it to be instant
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    I think the problem may be in the MoveR and MoveL methods. If you want to use TouchPhase.Moved, if your move methods do something like this
    transform.position += new Vector3(1,0,0);
    , you need to modify it by the frame time, so that you get a consistent and understandable speed. That would look like this
    transform.position += new Vector3(1,0,0) * Time.deltaTime;
     
  3. xxtickxtockxx

    xxtickxtockxx

    Joined:
    Jan 10, 2019
    Posts:
    16
    it's not the problem ..the problem is the late response check the rocket video to understand what I mean.
    when he lifts his finger it moves I want it to move before you lift your finger
     
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Oh, I get it. Try this
    Code (csharp):
    1. Touch t = Input.GetTouch(0);
    2. if(t.phase == TouchPhase.Began)
    3.    touchStart = touchEnd = t.position;
    4. if(t.phase == TouchPhase.Moved)
    5. {
    6.    touchEnd = t.position;
    7.    Vector2 diff = touchEnd - touchStart;
    8.    diff = new Vector2(diff.x / Screen.width, diff.y / Screen.height); // Normalize the difference relative to screen space
    9.    if(diff.magnitude > 0.1f) // Is the swipe 10% or more of the screen?
    10.    {
    11.       touchStart = touchEnd; // Change the swipe starting position.
    12.       if(Mathf.Abs(diff.x) > Mathf.Abs(diff.y))
    13.       {
    14.          if(diff.x > 0)
    15.             MoveR();
    16.          else
    17.             MoveL();
    18.       } else
    19.       {
    20.          if(diff.y > 0)
    21.             MoveU();
    22.          else
    23.             MoveD();
    24.       }
    25.    }
    26. }
     
  5. xxtickxtockxx

    xxtickxtockxx

    Joined:
    Jan 10, 2019
    Posts:
    16
    t
    THAT WORKS... But can i make so the move method happen once only for example if i swipe 20% screen the move method doesnt activate twice but only once.

    Man Thank you really i was searching for this for past week...
     
  6. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Yep, just need to make an adjustment
    Code (csharp):
    1.  // Instance variable
    2. private bool canSwipe = true;
    3.  
    4. // ...
    5.  
    6. // Inside the method
    7. Touch t = Input.GetTouch(0);
    8. if(t.phase == TouchPhase.Began)
    9. {
    10.    touchStart = touchEnd = t.position;
    11.    canSwipe = true; // Once a new touch starts, we can swipe again
    12. }
    13. if(t.phase == TouchPhase.Moved && canSwipe) // Only allow moving if we haven't done it this swipe yet
    14. {
    15.    touchEnd = t.position;
    16.    Vector2 diff = touchEnd - touchStart;
    17.    diff = new Vector2(diff.x / Screen.width, diff.y / Screen.height); // Normalize the difference relative to screen space
    18.    if(diff.magnitude > 0.1f) // Is the swipe 10% or more of the screen?
    19.    {
    20.       canSwipe = false; // Since we are going to move, disable swiping until the user starts another touch
    21.       if(Mathf.Abs(diff.x) > Mathf.Abs(diff.y))
    22.       {
    23.          if(diff.x > 0)
    24.             MoveR();
    25.          else
    26.             MoveL();
    27.       } else
    28.       {
    29.          if(diff.y > 0)
    30.             MoveU();
    31.          else
    32.             MoveD();
    33.       }
    34.    }
    35. }
     
  7. xxtickxtockxx

    xxtickxtockxx

    Joined:
    Jan 10, 2019
    Posts:
    16
    Mmmm last question can you make it the amount of pixel rather than a percentage?

    and thank you ever so much !!
     
  8. xxtickxtockxx

    xxtickxtockxx

    Joined:
    Jan 10, 2019
    Posts:
    16
    what does " diff.magnitude " mean?
     
  9. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You can, but the number of pixels will change from device to device, so on some phones 100 pixels will be large, but on others 100 pixels is very small. If you are sure you want to do that, then you can delete this line
    diff = new Vector2(diff.x / Screen.width, diff.y / Screen.height);
    , which is converting diff from pixels into percentages, and then change this line
    if(diff.magnitude > 0.1f)
    to something like
    if(diff.magnitude > 100) // Number of pixels instead of percentage


    diff is the vector going from touchStart to touchEnd. diff.magnitude is then the distance between the start and end of the swipe. Generally, the magnitude of a vector is how long the vector is. So in the code above, diff.magnitude means "the length of the swipe, as a percentage of the screen". If you delete the code that is making it a percentage, then diff.magnitude will mean "the length of the swipe in pixels".
     
    Last edited: Jan 15, 2019
  10. xxtickxtockxx

    xxtickxtockxx

    Joined:
    Jan 10, 2019
    Posts:
    16
    so its baiscly magnitude is the same is vector3.destance?
     
  11. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Only in this case. magnitude is the length of the vector. In this case, the vector in question happens to be the vector going from the start to the end. So
    diff.magnitude == Vector3.Distance(touchStart, touchEnd)
    . You could say that the magnitude of a vector is the distance between the vector's start and stop points.
     
  12. Vegito2367

    Vegito2367

    Joined:
    Dec 17, 2020
    Posts:
    2
    Whats the difference, i dont get it