1
\$\begingroup\$

I Have a character in my game that must go when player hold finger at the screen, but when I swipe character first go, and then do swipe. (for example jumping - character first go, and only then jump). This my code for touch:

 if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);

        if (touch.phase == TouchPhase.Began)
        {
            startTime = Time.time;
            startPos = touch.position;
        }
        else if (touch.phase == TouchPhase.Ended)
        {
            endTime = Time.time;
            endPos = touch.position;
            swipeDist = (endPos - startPos).magnitude;
            swipeTime = endTime - startTime;

            if (swipeTime < maxTime && swipeDist > minSwipeDist)
            {
                swipe();
            }
        }
        else if (touch.phase == TouchPhase.Stationary && isTap == 0)
        {
           hold();
        }
    }

This my code for a hold:

  void hold()
{

        moveVelocity = 3f;


}

And for swipe:

void swipe()
{
    Vector2 distance = endPos - startPos;
        if (Mathf.Abs(distance.x) > Mathf.Abs(distance.y))
    { 
           if (distance.x > 0)
        {
            Debug.Log("Right swipe");
        }
        if (distance.x < 0)
        {
           Debug.Log("Left swipe");  
        }
    }
    if (Mathf.Abs(distance.x) < Mathf.Abs(distance.y))
    {
        if (distance.y > 0)
        {
            Debug.Log("Up swipe");
        }
        if (distance.y < 0)
        {
            Debug.Log("Down swipe");
        }
    }
}

My question is: When player swipe left character first go, and only then do swipe left. I want player swipe, without going forward. How can I do this?

\$\endgroup\$
0
\$\begingroup\$

it looks like you need to wait just a bit, and collect some position data where you call Hold, to make sure the player didn't intend to swipe.

Something like this:

endPos=touch.position;
swipeDist=(endPos-startPos).magnitude;
holdTime=endTime-startTime;
if(holdTime>minHoldTime&&swipeDist<holdThreshold)hold();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.