0
\$\begingroup\$

I'm trying to make a game where your tap doesn't count until you've released your click, or stop tapping.

The weird thing is, it doesn't work in game, but it works for animation.

public class Swipe : MonoBehaviour {

    public Animator anim;
    public bool tap, swipeUp, swipeDown, swipeLeft, swipeRight, hold;
    public bool isDragging = false;
    public Vector2 startTouch, swipeDelta;
    public float animTime;
    public Life life;



    private void Update()
    {
        tap = swipeUp = swipeDown = swipeLeft = swipeRight = false;

        anim.SetBool("Hold", isDragging);


        #region Standalone Input
        if (Input.GetMouseButtonDown(0))
        {

            isDragging = true;
            startTouch = Input.mousePosition;
            //Debug.Log("Tapped");
        }
        else if (Input.GetMouseButtonUp(0))
        {
            tap = true;
            isDragging = false;
            Reset();
        }

        #endregion

        #region Mobile Inputs
        if (Input.touches.Length >= 1)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                // tap = true;
                isDragging = true;
                startTouch = Input.touches[0].position;
            }
            else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
            {
                tap = true;
                isDragging = false;
                Reset();
            }
        }


        #endregion

        //calculating the distance
        swipeDelta = Vector2.zero;
        if (startTouch != Vector2.zero)
        {
            if (isDragging)
            {
                if (Input.touches.Length > 0)
                    swipeDelta = Input.touches[0].position - startTouch;
                else if (Input.GetMouseButton(0))
                    swipeDelta = (Vector2)Input.mousePosition - startTouch;
            }
        }

        //did we cross the deadzone?
        if (swipeDelta.magnitude > 125)
        {
            //which direction?
            float x = swipeDelta.x;
            float y = swipeDelta.y;
            if (Mathf.Abs(x) > Mathf.Abs(y))
            {
                //left or right
                if (x < 0)
                {
                    swipeLeft = true;

                }
                else
                {
                    swipeRight = true;
                }
            }
            else
            {
                // Up or down?
                if (y < 0)
                {
                    swipeDown = true;
                }
                else
                {
                    swipeUp = true;
                }
            }
        }


        #region Animation
        if (Tap)
        {
            anim.SetTrigger("Tap");
        }

        if (SwipeDown)
        {
            anim.SetTrigger("Down");

        }

        if (SwipeUp)
        {
            anim.SetTrigger("Up");
        }

        if (SwipeLeft)
        {
            anim.SetTrigger("Left");
        }

        if (SwipeRight)
        {
            anim.SetTrigger("Right");
        }

        #endregion
    }


    public void Reset()
    {
        startTouch = swipeDelta = Vector2.zero;
        isDragging = false;
    }

    public Vector2 SwipeDelta { get { return swipeDelta; } }
    public bool Tap { get { return tap; } }
    public bool SwipeUp { get { return swipeUp; } }
    public bool SwipeDown { get { return swipeDown; } }
    public bool SwipeLeft { get { return swipeLeft; } }
    public bool SwipeRight { get { return swipeRight; } }
}
\$\endgroup\$
0
\$\begingroup\$
TouchPhase.Ended

Is true when the player lifts their finger off of the screen.

TouchPhase.Cancelled

Is true when the game is interrupted while the player is still touching the screen. You can use that for whichever functionality you like to implement.

\$\endgroup\$
  • \$\begingroup\$ the thing is the animation works, but the boolean doesn't really works. when tapped, you can see that the boolen turns to true sometimes(as it resets after released/swiped), but it just doesn't work... \$\endgroup\$ – Chan Ming Jun 24 at 19:51

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.