[HELP] Swipe movement

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.

    if (Input.touchCount > 0)
            {
                Touch myTouch = Input.touches[0];
    
                //Check if the phase of that touch equals Began
                if (myTouch.phase == TouchPhase.Began)
                {
                    touchStart = myTouch.position;
                    touchEnd = myTouch.position;
                }
                if (myTouch.phase == TouchPhase.Moved)
                {
                    touchEnd = myTouch.position;
    
    
                }
                if (myTouch.phase == TouchPhase.Ended || Mathf.Abs(Vector3.Distance(touchEnd, touchEnd)) > touchDistance)
                {
                    touchEnd = myTouch.position;
                    float x = touchEnd.x - touchStart.x;
                    float y = touchEnd.y - touchStart.y;
    
                    if (Mathf.Abs(x) > Mathf.Abs(y))
                    {
                        if (x > 0)
                            MoveR();
                        else
                            MoveL();
                    }
                    else
                    {
                        if (y > 0)
                            MoveU();
                        else
                            MoveD();
                    }
                }
            }

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

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;

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

Oh, I get it. Try this

Touch t = Input.GetTouch(0);
if(t.phase == TouchPhase.Began)
   touchStart = touchEnd = t.position;
if(t.phase == TouchPhase.Moved)
{
   touchEnd = t.position;
   Vector2 diff = touchEnd - touchStart;
   diff = new Vector2(diff.x / Screen.width, diff.y / Screen.height); // Normalize the difference relative to screen space
   if(diff.magnitude > 0.1f) // Is the swipe 10% or more of the screen?
   {
      touchStart = touchEnd; // Change the swipe starting position.
      if(Mathf.Abs(diff.x) > Mathf.Abs(diff.y))
      {
         if(diff.x > 0)
            MoveR();
         else
            MoveL();
      } else
      {
         if(diff.y > 0)
            MoveU();
         else
            MoveD();
      }
   }
}

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…

Yep, just need to make an adjustment

 // Instance variable
private bool canSwipe = true;

// ...

// Inside the method
Touch t = Input.GetTouch(0);
if(t.phase == TouchPhase.Began)
{
   touchStart = touchEnd = t.position;
   canSwipe = true; // Once a new touch starts, we can swipe again
}
if(t.phase == TouchPhase.Moved && canSwipe) // Only allow moving if we haven't done it this swipe yet
{
   touchEnd = t.position;
   Vector2 diff = touchEnd - touchStart;
   diff = new Vector2(diff.x / Screen.width, diff.y / Screen.height); // Normalize the difference relative to screen space
   if(diff.magnitude > 0.1f) // Is the swipe 10% or more of the screen?
   {
      canSwipe = false; // Since we are going to move, disable swiping until the user starts another touch
      if(Mathf.Abs(diff.x) > Mathf.Abs(diff.y))
      {
         if(diff.x > 0)
            MoveR();
         else
            MoveL();
      } else
      {
         if(diff.y > 0)
            MoveU();
         else
            MoveD();
      }
   }
}

Mmmm last question can you make it the amount of pixel rather than a percentage?

and thank you ever so much !!

what does " diff.magnitude " mean?

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”.

so its baiscly magnitude is the same is vector3.destance?

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.

Whats the difference, i dont get it